Skip to content

Instantly share code, notes, and snippets.

@pmatatias
Created July 30, 2020 00:07
Show Gist options
  • Select an option

  • Save pmatatias/fc87f8a4064c12419bbf1479a1bd207d to your computer and use it in GitHub Desktop.

Select an option

Save pmatatias/fc87f8a4064c12419bbf1479a1bd207d to your computer and use it in GitHub Desktop.
class Solution {
public:
int countPrimes(int n) {
if (n<= 2) return 0;
int count = n/2;
bool notPrime [n];
// for (int i=0; i<n; i++){
// notPrime[i]= false;
// }
memset(notPrime,false,n);
for (int i = 3; i*i<n; i+=2){
if(notPrime[i]) continue;
for (int j= i*i; j<n; j+= i*2){
if(!notPrime[j]){
notPrime[j]= true;
count--;
}
}
}
return count;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment