-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsieve.cpp
More file actions
49 lines (43 loc) · 779 Bytes
/
sieve.cpp
File metadata and controls
49 lines (43 loc) · 779 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
41
42
43
44
45
46
47
48
49
/**
@version 1.21 2004-08-03
@author Cay Horstmann
*/
#include <bitset>
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
const int N = 2000000;
clock_t cstart = clock();
bitset<N + 1> b;
int count = 0;
int i;
for (i = 2; i <= N; i++)
b.set(i);
i = 2;
while (i * i <= N)
{
if (b.test(i))
{
count++;
int k = 2 * i;
while (k <= N)
{
b.reset(k);
k += i;
}
}
i++;
}
while (i <= N)
{
if (b.test(i))
count++;
i++;
}
clock_t cend = clock();
double millis = 1000.0 * (cend - cstart) / CLOCKS_PER_SEC;
cout << count << " primes\n" << millis << " milliseconds\n";
return 0;
}