-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem 10
More file actions
49 lines (40 loc) · 891 Bytes
/
Copy pathproblem 10
File metadata and controls
49 lines (40 loc) · 891 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
/*The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
*/
#include<iostream>
#include<cmath>
#include<vector>
#include <ctime>
using namespace std;
bool checkprime(int x, const vector<int>& primes){
for(int prime: primes){
if (x % prime == 0){
return false;
}
if(prime > pow(x, 0.5)){
return true;
}
}
return true;
}
vector<int> all_primes(int upper_bound){
vector<int> primes = {2};
for(int i = 3; i <= upper_bound; i+=2){
if(checkprime(i, primes)){
primes.push_back(i);
}
}
return primes;
}
int main() {
std::time_t t0 = std::time(nullptr);
const int upper_bound = 2000000;
long long sum = 0;
for(auto prime : all_primes(upper_bound)){
sum += prime;
}
cout << sum << endl;
std::time_t t1 = std::time(nullptr);
cout << "time: " << t1 - t0;
return 0;
}