标签:
Description:
Count the number of prime numbers less than a non-negative number, n.
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
埃拉托斯特尼筛法
int countPrimes(int n) { vector<bool> prime(n, true); prime[0] = false, prime[1] = false; for (int i = 0; i < sqrt(n); ++i) { if (prime[i]) { for (int j = i*i; j < n; j += i) { prime[j] = false; } } } return count(prime.begin(), prime.end(), true); }
标签:
原文地址:http://www.cnblogs.com/sdlwlxf/p/5136303.html