Advertisement
Zeinab_Hamdy

Untitled

Dec 30th, 2022
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.43 KB | None | 0 0
  1. class Solution {
  2. public:
  3.  
  4.     int countPrimes(int N) {
  5.         int ans=0;
  6.      
  7.      vector < bool > is_prime(N , true);
  8.       is_prime[0] = is_prime[1] = false;
  9.  
  10.     for(long long i = 2; i * i < N; i++)
  11.         if(is_prime[i])
  12.             for(long long j = i * i; j < N; j += i)
  13.                 is_prime[j] = false;
  14.  
  15.     for(int i = 2; i < N ; i++)
  16.         if(is_prime[i])
  17.             ans++;
  18.  
  19.         return ans;
  20.     }
  21. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement