-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0010.cpp
47 lines (44 loc) · 1.03 KB
/
0010.cpp
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
#include <iostream>
#include <array>
#include <vector>
#include <future>
#include <functional>
template <long int smax>
long int sumprimes() {
std::array<bool, smax> sieve;
sieve.fill(true);
long int i = 2;
long int sum = 2;
while (i < smax - 1) {
long int j = i+i;
while (j < smax - 1) {
// mark all the multiples of i in the sieve as not-prime
sieve[j] = false;
j += i;
}
while (i < smax - 1) {
// find the next value in the sieve that is still true (meaning it's prime)
i += 1;
if (sieve[i] == true) {
break;
}
}
if (i >= smax / 2) {
// after we have checked sqrt(smax) numbers, we wont find any more
// so everything true value left in the sieve will be a prime
while (i < smax - 1) {
if (sieve[i] == true) {
sum += i;
}
i += 1;
}
} else {
sum += i;
}
}
return sum;
}
void fn0() {
std::cout << sumprimes<2000000>() << std::endl;
}
std::vector<std::function<void()>> progs = { fn0 };