欧拉项目第7题 10001st prime

来源:互联网 发布:阿里云 共享存储 编辑:程序博客网 时间:2024/05/24 04:16

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10 001st prime number?

求10001个质数,

思路是这样的,将2放入数组,从3开始的奇数,与数组内的质数进行整除,全部不能整除就是质数,放入数组

 public static void main(String[] args) {
        ArrayList<Integer> x = new ArrayList<Integer>();
        x.add(2);
        int index = 0;
        int end=10000;
        for(int i=3;;i=i+2){
            Boolean yh = true;
            for(Integer ix : x) {
                if(i%ix==0) {
                    yh=false;
                    break;
                }
            }
            if(yh) {
                index++;
                x.add(i);
                //System.out.print(i+"  ");
                yh=true;
            }
            if(index == end)
                break;
        }
        System.out.println(x.get(end));
    }

0 0
原创粉丝点击