Skip to content

Commit

Permalink
Merge pull request #1590 from raj-hero/master
Browse files Browse the repository at this point in the history
Created leetcode-Count_Primes.cpp
  • Loading branch information
fineanmol authored Oct 1, 2022
2 parents c357936 + d6c0388 commit cdb8526
Showing 1 changed file with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// question:https://leetcode.com/problems/count-primes/submissions/812562322/
class Solution {
public:
int countPrimes(int n)
{
// sieve algorithm
vector<bool> primes(n+1,true);
primes[0]=primes[1]=false;
for(int i=2;i<=n;++i){
if(primes[i]==true){
for(int j=2*i;j<=n;j+=i){
primes[j]=false;
}
}
}
// queries
int ct=0;
for(int i=0;i<n;++i)
{
if(primes[i]==true) ++ct;
}
return ct;
}
};

0 comments on commit cdb8526

Please sign in to comment.