题目链接:https://leetcode.com/problems/count-primes/
题目:
Description:
Count the number of prime numbers less than a non-negative number, n.
思路:
埃拉托色尼选筛法
算法:
public int countPrimes(int n) {
boolean c[] = new boolean[n];
for (int i = 2; i * i < n; i++) {
if(!c[i]){
for (int j = i+i; j < n; j += i) {
if(!c[j])
c[j] = true;
}
}
}
int count = 0;
for (int i = 2; i < n; i++) {
if (!c[i])
count++;
}
return count;
}