您的位置:首页 > 其它

Count Primes

2015-06-08 22:11 375 查看
Count the number of prime numbers less than a non-negative number, n.

class Solution {
public:
int countPrimes(int n) {

if(n <= 2) return 0;

int res = 1;
for(int i = 3;i < n; i+=2){
int bound = sqrt(i),j;
for(j = 3; j <= bound; j+=2)
{
if(i%j == 0) break;
}
if(j > bound) ++res;
}

return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: