204. Count Primes

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;
    }

 

posted @ 2016-09-14 05:52  咖啡中不塌缩的方糖  阅读(102)  评论(0编辑  收藏  举报