for-Schleife zum Finden der Primzahlen
Ich versuche, diesen Code auszuführen, um die Summe aller Primzahlen unter 2 Millionen zu drucken. Diese Schleife endet nie. Kann mir jemand sagen, was mit dem Code nicht stimmt? Es scheint jedoch mit kleineren Zahlen zu funktionieren.
public static void main(String[] args) {
long result = 1;
for(int i=0; i<2000000; i++) {
if(isPrime(i)) {
result+= i;
}
}
System.out.println(result);
}
private static boolean isPrime(long n) {
boolean result = false;
for(long i=2; i<(long)Math.sqrt(n); i++) {
if(n%i == 0) {
result = false;
break;
}
else result = true;
}
return result;
}