首页 > 其他 > 详细

204. Count Primes

时间:2016-09-14 07:17:14      阅读:243      评论:0      收藏:0      [点我收藏+]

Description:

Count the number of prime numbers less than a non-negative number, n.

埃拉托斯特尼筛法

public int CountPrimes(int n) {
        if(n<3) return 0;
        bool[] flag = new bool[n+1];
        int count =0;
        for(int i = 2; i< n; i++)
        {
            if(flag[i] == true) continue;
            
            else
            {
                count++;
                for(int j =2 ; j*i <= n; j++)
                {
                    flag[j*i] = true;
                }
            }
        }
        return count;
    }

 

204. Count Primes

原文:http://www.cnblogs.com/renyualbert/p/5870495.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!