10 Summation of primes - Project Euler

来源:互联网 发布:淘宝助理新建宝贝 编辑:程序博客网 时间:2024/05/21 08:49
package xxx.xxx.xxx;


import java.math.BigInteger;


/*
 * The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
 * Find the sum of all the primes below two million.
 */


public class SummationOfPrimes {
private void compute(int range){
BigInteger sum = BigInteger.valueOf(2);
for(int i = 3; i< range; i++){
boolean isPrime = true;
for(int j = 2; j<i; j++){
if(i%j==0){
isPrime = false;
break;
}
}
if(isPrime){
System.out.println(i);
sum = sum.add(BigInteger.valueOf(i));
}
}
System.out.println(sum);

}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SummationOfPrimes summationOfPrimes = new SummationOfPrimes();
summationOfPrimes.compute(2000000);
}


}
0 0
原创粉丝点击