Leetcode 204: Count Primes

Description:

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

 

 1 public class Solution {
 2     public int CountPrimes(int n) {
 3         var notPrime = new bool[n + 1];
 4         int count = 0;
 5         
 6         for (int i = 2; i < n; i++)
 7         {
 8             if (!notPrime[i])
 9             {
10                 count++;
11             }
12             
13             for (int j = 2; i * j <= n; j++)
14             {
15                 notPrime[i * j] = true;
16             }
17         }
18         
19         return count;
20     }
21 }

 

posted @ 2017-11-29 07:24  逸朵  阅读(137)  评论(0)    收藏  举报