-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathsolve.cpp
More file actions
40 lines (40 loc) · 748 Bytes
/
solve.cpp
File metadata and controls
40 lines (40 loc) · 748 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
bool isPrime(int n)
{
//asssert(n > 0);
if (n <= 3)
return true;
for (int i = 2; i <= sqrt(n); ++i) {
if (n % i == 0)
return false;
}
return true;
}
class Solution {
public:
int countPrimes(int n) {
vector<bool> flags(n, true);
flags[0] = false;
flags[1] = false;
int sqr = sqrt(n - 1);
for (int i = 2; i <= sqr; ++i) {
if (flags[i]) {
for (int j = i * i; j < n; j += i)
flags[j] = false;
}
}
return count(flags.begin(), flags.end(), true);
}
};
int main(int argc, char **argv)
{
Solution solution;
int n;
while (scanf("%d", &n) != EOF) {
printf("Pi(%d) = %d\n", n, solution.countPrimes(n));
}
return 0;
}