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