Project Euler: Problem 10

By |

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

Find the sum of all the primes below two million.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
long result = 0;
boolean[] gestrichen = new boolean[limit];
for (int i=2; i<gestrichen.length; i++) {
	gestrichen[i] = false;
}
 
for (int i=2; i<gestrichen.length; i++) {
	if (!gestrichen[i]) {
		System.out.println(i + " ");
		result += (long)i;
		for (int j=2*i; j<limit; j=j+i) {
			gestrichen[j] = true;
		}
	}
}
System.out.println("=============");
System.out.println(result);

Lösung: 142913828922