Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int countPrimes(int n) {
- if (n == 0) {
- return 0;
- }
- bool notPrime[n] = {false};
- int cnt = 0;
- const int limit = sqrt(n);
- for(int i = 2; i < n; ++i) {
- if (!notPrime[i]) {
- ++cnt;
- if(i <= limit) {
- for (int j = 2; j * i < n; ++j) {
- notPrime[j * i] = true;
- }
- }
- }
- }
- return cnt;
- }
- };
Add Comment
Please, Sign In to add comment