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 }

浙公网安备 33010602011771号