我正在尝试解决 Project Euler 的第 10个问题,但由于某种原因,我无法正确解决。我在编程和 Java 方面真的很陌生,所以我不明白为什么它不起作用。问题的重点是找出所有低于 2,000,000 的素数之和。
/* The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
*/
public static void main(String[] args){
long n = 1;
long sum = 0;
long limit;
System.out.println("Enter the limit of n: ");
limit = TextIO.getlnLong(); //TextIO is another input method
while (limit <= 0){
System.out.println("Enter the limit of n (must be positive): ");
limit = TextIO.getlnLong();
}
while (n < limit){
n++;
if (n % 2 != 0 && n % 3 != 0 && n % 5 != 0 && n % 7 != 0 && n != 1 || n == 2 || n == 3 || n == 5 || n == 7){ //this is my prime checking method, might be flawed
sum = sum + n;
System.out.println(+sum);
} //end if
}//end while
System.out.println("The sum of the primes below 2,000,000 is: " +sum);
} //end of main