Problem 10 of Find the sum of all the primes below two million.

来源:互联网 发布:华东交大软件学院吧 编辑:程序博客网 时间:2024/06/05 21:08

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

public class BigPrime {public static void main(String args[]){long j=0;for(long i=2;i<2000000;i++){if(isPrime(i)){j+=i;}}System.out.println(j);}public static boolean isPrime(long n){    for(int i = 2; i * i <= n; i++){  if(n % i == 0)   return false;  }    return true;}}

Answer:
142913828922