欧拉项目第十题 Summation of primes

来源:互联网 发布:关键词出价的算法 编辑:程序博客网 时间:2024/06/08 06:29

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

Find the sum of all the primes below two million.

求小于2000000的质数之和

public static void main(String[] args) {        ArrayList<Integer> x = new ArrayList<Integer>();        long xx = 2;        for(int i=3;i<2000000;i=i+2){            Boolean yh = true;            for(Integer ix : x) {                if(ix > Math.sqrt(i)) {                    break;                }                if(i%ix==0) {                    yh=false;                    break;                }            }            if(yh) {                xx+=i;                x.add(i);                yh=true;            }        }        System.out.println(xx);    }


0 0
原创粉丝点击