Discription
Count the number of prime numbers less than a non-negative number, n.
Solution
int countPrimes(int n){ //埃拉託斯特尼篩法 2到n所有素數
vector<bool> nums(n, true);
for (int i = 2; i < sqrt(n); i++){
if (!nums[i]) continue;
int j = i*i;
while (j < n){
nums[j] = false;
j += i;
}
}
int result = 0;
for (int i = 2; i < n; i++){
if (nums[i])
result++;
}
return result;
}
GitHub-Leetcode:https://github.com/wenwu313/LeetCode