Advertisement
nikunjsoni

204

May 10th, 2021
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int countPrimes(int n) {
  4.         bool isPrime[n+1];
  5.         for(int i = 2; i < n; i++)
  6.             isPrime[i] = true;
  7.    
  8.         for(int i = 2; i * i < n; i++) {
  9.             if(!isPrime[i]) continue;
  10.             for(int j = i * i; j < n; j += i) {
  11.                 isPrime[j] = false;
  12.             }
  13.         }
  14.         int count = 0;
  15.         for(int i = 2; i < n; i++)
  16.             if (isPrime[i]) count++;
  17.        
  18.         return count;
  19.     }
  20. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement