-
Notifications
You must be signed in to change notification settings - Fork 0
/
Euler_50.cpp
55 lines (44 loc) · 1.62 KB
/
Euler_50.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
48
49
50
51
52
53
54
55
#include <numeric>
#include "Euler.h"
int Euler::ConsecutivePrimeSum()
{
std::vector<int> primes = EulerUtility::getPrimesUnderCeiling(1000000);
std::vector<int> primes2 = EulerUtility::getPrimesUnderCeilingIndexed(1000000);
int highestPotential = 0;
for (int i = 0; i < 600; ++i)
{
int potentialPrime = std::accumulate(primes.begin(), primes.begin() + 600 - i, 0);
if (potentialPrime < 1000000)
{
if (potentialPrime == primes2[potentialPrime])
{
highestPotential = potentialPrime;
int greatestLength = 600 - i;
for (int j = 1; ; ++j)
{
bool firstPass = true;
for (int k = 1; ; ++k)
{
potentialPrime = std::accumulate(primes.begin() + j, primes.begin() + 600 - i + j + k, 0);
if (potentialPrime < 1000000)
{
if ((potentialPrime == primes2[potentialPrime]) && (600 - i + k > greatestLength))
{
highestPotential = potentialPrime;
greatestLength = 600 - i + k;
}
firstPass = false;
}
else
{
if (firstPass)
return highestPotential;
break;
}
}
}
}
}
}
return 0;
}