码迷,mamicode.com
首页 > 其他好文 > 详细

204. Count Primes

时间:2016-09-14 07:19:03      阅读:162      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!