for loop finding the prime numbers

后端 未结 5 2060
生来不讨喜
生来不讨喜 2020-12-20 03:05

I am trying to run this code to print the sum of all the prime numbers less than 2 million. This loop is never ending. Can anyone tell me what is wrong with the code? It see

5条回答
  •  囚心锁ツ
    2020-12-20 03:42

    Tested and bug free Prime check function

    static boolean isPrime(int n) {
        if (n == 1) return false;
    
        for(int i = 2; i <= n/2; i++)
            if(n % i == 0)
                return false;
    
        return true;
    }
    

提交回复
热议问题