求质数

来源:互联网 发布:佳能手机打印软件 编辑:程序博客网 时间:2024/05/01 09:58
public static int countPrimes(int n) {    /**     * optimize the code, the non-prime numbers begin at p*p     */    int count = 0;    int[] store = new int[n+1];    for(int i=2;i<n;i++){    if(store[i]!=-1){    count++;    store[i] = 0;    for(int j=i*i;0<j && j<=n;j+=i){    store[j] = -1;    }    }    }        return count;}
0 0
原创粉丝点击