Tuesday, July 17, 2012

answer java code for eular problem 10

PROBLEM


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

Find the sum of all the primes below two million.

ANSWER



142913828922

JAVA CODE

public class Problem10 {

public static void main(String[]args){
primeGenerator();
}

private static void primeGenerator() {
int max=2000000;
BigInteger bg=new BigInteger("0");
for (int j = 2; j < max; j++) {
boolean prime=true;
for (int i = 2; i <= (int)Math.sqrt(j); i++) {
if(j%i==0 )
prime=false;
}
if(prime){
BigInteger add=new BigInteger(Integer.toString(j));
bg=bg.add(add);
}
}
System.out.println("ans="+bg);
}
}




0 comments:

Post a Comment