厄拉多塞筛法
class Solution { public: int countPrimes(int n) { int cnt = 0; vector<bool> signs(n, true); for (int i = 2; i < n; i++) { if (signs[i]) { cnt++; for (int j = i; j < n; j+=i) //排除非质数 signs[j] = false; } } return cnt; } };
厄拉多塞筛法
class Solution { public: int countPrimes(int n) { int cnt = 0; vector<bool> signs(n, true); for (int i = 2; i < n; i++) { if (signs[i]) { cnt++; for (int j = i; j < n; j+=i) //排除非质数 signs[j] = false; } } return cnt; } };