问题:
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
翻译:
求出所有小于2000000的质数的和
代码:
package projectEuler;
public class Problem10 {
private static int N = 2000000;
public static void main(String[] args) {
long sum = 0;
for (int i = 2; i <= N; i++) {
if (isPrime(i)) {
sum += i;
}
}
System.out.println("sum=" + sum);
}
static boolean isPrime(int n) {
for (int i = 2; i <= Math.sqrt(n); i++) {
if (0 == n % i) {
return false;
}
}
return true;
}
}