Created
July 30, 2020 00:07
-
-
Save pmatatias/fc87f8a4064c12419bbf1479a1bd207d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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