Project Euler #10 Java not working -


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

find sum of primes below 2 million.

            public class problem10             {                 public static void main(string[] args)                 {                     int sum = 17;                     for(long i=11; i<2000000; i+=2)                     {                         if(isprime(i)) sum += i;                     }                     system.out.println(sum);                      }                  public static boolean isprime(long n)                 {                     if(n==2) return true;                     if(n%2 == 0) return false;                     for(long i=3; i<= math.sqrt(n); i+=2)                     {                         if(n%i == 0) return false;                     }                     return true;                 }             } 

i don't see wrong this. keeps printing 1179908154 incorrect.

your sum int, integer overflow occurring. if change datatype long, answer changes 142913828922, longer maximum possible int. that appears correct answer.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -