Skip to content

Instantly share code, notes, and snippets.

@s4553711
Created February 26, 2018 15:19
Show Gist options
  • Save s4553711/771cee8309eeeebfa51f1f7631192ff2 to your computer and use it in GitHub Desktop.
Save s4553711/771cee8309eeeebfa51f1f7631192ff2 to your computer and use it in GitHub Desktop.
class Solution {
public:
int countPrimes(int n) {
if (n == 1 || n == 0) return 0;
vector<bool> passed(n, true);
passed[0] = false, passed[1] = false;
for(int i = 0; i < sqrt(n); i++) {
if (passed[i]) {
for(int j = i * i; j < n; j += i) {
passed[j] = false;
}
}
}
return count(passed.begin(), passed.end(), true);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment