`
尘枉_yjava
  • 浏览: 73930 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

1-n 以内的素数

 
阅读更多
static void ListPrime(int n) { 
        /**
         * false为质数,true为合数
         */ 
        boolean[] primeList = new boolean[n + 1]; 
 
        for (int i = 2; i <= n; i++) { 
            if (!primeList[i]) { 
                int j = i * i; 
                if (j > n) 
                    break; 
                if (i > 2) { 
                    while (j <= n) { 
                        primeList[j] = true; 
                        j = j + i + i; 
                    } 
                } else { 
                    while (j <= n) { 
                        primeList[j] = true; 
                        j = j + i; 
                    } 
                } 
            } 
        } 
        List<Integer> ret = new ArrayList<Integer>(10000); 
        ret.add(2); 
        for (int i = 3; i <= n;) { 
            if (!primeList[i]) { 
                //System.out.print(i + " ");   
                ret.add(i); 
            } 
            i += 2; 
        } 
        System.out.println(ret.size());
    } 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics