-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem7.cpp
More file actions
54 lines (38 loc) · 1.05 KB
/
Copy pathproblem7.cpp
File metadata and controls
54 lines (38 loc) · 1.05 KB
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
50
51
52
53
54
#include <iostream>
#include <cmath>
#include <vector>
#include <time.h>
#define PTYPE unsigned int
// 2 need not be included here since is_prime pre-emptively filters out multiples of tow
std::vector<PTYPE> primes = {3};
bool is_prime(PTYPE number) {
if (number <= 2) {return true;}
//if (number % 2 == 0) {return false;}
if (number == 3) {return true;}
size_t l = primes.size();
size_t actual_l = sqrt(number) + 1;
for (size_t i = 0; i < l; ++i) {
size_t j = primes[i];
if (j > actual_l) {
break;
}
if (number % j == 0) {
return false;
}
}
primes.push_back(number);
return true;
}
int main() {
clock_t start = clock();
PTYPE counter = 5;
while(primes.size() + 1 < 10002) {
is_prime(counter);
counter = counter + 2;
}
std::cout << primes[9999] << std::endl;
clock_t end = clock();
double duration = (double)(end - start)/CLOCKS_PER_SEC;
std::cout << "Time taken: " << duration << std::endl;
return 0;
}