-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path050.cpp
61 lines (52 loc) · 1.24 KB
/
050.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
56
57
58
59
60
61
#include <iostream>
#include <cmath>
#include <vector>
bool isPrime(int n) {
if (n < 4) return true;
if (n % 2 == 0) return false;
int end = floor(sqrt(n));
int divisor = 3;
do {
if (n % divisor == 0) return false;
divisor = divisor + 2;
} while (divisor <= end);
return true;
}
int sumOfRange(int start, int count, std::vector<int> array) {
int bound = start + count;
int sum = 0;
for (int i = start; i < bound; i++) {
sum += array[i];
}
return sum;
}
bool isIn(int n, std::vector<int> array) {
for (int item : array) {
if (n == item) return true;
}
return false;
}
int main() {
std::vector<int> primes;
for (int n = 2; n < 1000000; n++) {
if (isPrime(n)) {
primes.push_back(n);
}
}
unsigned long count = primes.size();
int maxTermCount = 21;
int termCount = 0;
int sum = 0;
for (int i = 0; i < count - maxTermCount ; i++) {
if (termCount == maxTermCount) break;
for (termCount = maxTermCount; termCount < count - i; termCount++) {
sum = sumOfRange(i, termCount, primes);
if (sum > 1000000) break;
if (isIn(sum, primes)) {
std::cout << sum << " " << i << " " << termCount << std::endl;
maxTermCount = termCount;
}
}
}
return 0;
}