-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp10.cpp
45 lines (43 loc) · 918 Bytes
/
p10.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
#include <iostream>
#include <string>
#include <vector>
// Summation of primes
int main()
{
int max = 2000000;
long sum = 2;
std::vector<int> primes = {2};
long slow = 0;
long fast = 0;
for (int i = 3; i <= max; i += 2)
{
bool prime = true;
for (int p : primes)
{
slow++;
if (i % p == 0)
break;
}
for (int p : primes)
{
fast++;
if (p * p > i)
break;
if (i % p == 0)
{
prime = false;
break;
}
}
if (prime)
{
sum += i;
primes.push_back(i);
}
}
std::cout << primes.size() << std::endl;
std::cout << "slow " << slow << std::endl;
std::cout << "fast " << fast << std::endl;
std::cout << sum << std::endl;
return 0;
}