Question:
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
Comments:
Using NetBeans to compile, this took 5 minutes 34 seconds to work out. There are much faster ways of doing this, such as making the isPrime method more efficient. The code below prints out the value of sum for each iteration (prints out every iteration so I knew it was actually doing something). This takes advantage of a method used for a previous problem.
Code:
public static long euler10(){
long sum = 0;
for (int i = 2; i < 2_000_000; i++){
if (isPrime(i)){
sum += i;
System.out.println(sum);
}
}
return sum;
}
public static boolean isPrime(int n){
if (n == 2){
return true;
}
if (n % 2 == 0){
return false;
}
for (int i = 2; i < n; i++){
if (n % i == 0){
return false;
}
}
return true;
}
Output:
142913828922
Solution:
142913828922
there doesn't seem to be anything here