java - Find the sum of all the primes below two million.My program doesn't work for very big numbers -


this code finding sum of primes.it works low numbers if it's 2000000(2 million) never ends.anybody can me?

import java.math.biginteger; public class problem010{     public static void main(string[] args) {          biginteger sum = new biginteger("2");          //for (int i=3; i<2000000; i++) {         for(int i=3; i<10; i++){             (int j=2; j<i; j++){                 if (i % j == 0)                      break;                 else if (i == j+1){                     sum = sum.add(biginteger.valueof(i));                 }             }         }         system.out.println("sum  = "+sum);      } } 

your answer 142913828922 how?

i changed algorithm little bit:

public static void main(string[] args) {      biginteger sum = new biginteger("2");     boolean isprime = true;     (int i=3; i<2000000; i++) {     double aa = math.sqrt((double)i);         (int j=2; j<=aa; j++){             if (i % j == 0){                  isprime = false;                 break;             }         }         if(isprime){             sum = sum.add(biginteger.valueof(i));         }         isprime = true;     }     system.out.println("sum  = "+sum);  } 

instead of going through numbers 2 i go 2 sqrt(i) , improve code running time lot :)


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -