-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectEuler050.cpp
More file actions
87 lines (67 loc) · 1.59 KB
/
Copy pathprojectEuler050.cpp
File metadata and controls
87 lines (67 loc) · 1.59 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* Problem 50
Consecutive Prime Sum
The prime 41, can be written as the sum of six consecutive primes:
41 = 2 + 3 + 5 + 7 + 11 + 13
This is the longest sum of consecutive primes that adds to a prime
below one-hundred.
The longest sum of consecutive primes below one-thousand that adds
to a prime, contains 21 terms, and is equal to 953.
Which prime, below one-million, can be written as the sum of the most
consecutive primes?
*/
#include <iostream>
#include <vector>
using namespace std;
bool isPrime (int x) {
if (x == 1) {
return false;
}
if (x > 2 && x % 2 == 0) {
return false; // Other than 2, even numbers are not prime.
}
for (int divisor = 2; divisor <= floor (double (x / 2)); divisor++) {
if (x % divisor == 0) {
return false;
}
}
return true;
}
// Returns a vector of all the 4-digit primes.
vector <int> getPrimes () {
vector <int> primes;
for (int i = 2; i < 4000; i++) {
if (isPrime (i)) {
primes.push_back (i);
}
}
return primes;
}
int main () {
vector <int> primes = getPrimes ();
int mostOverall = 0;
int pOverall = 0;
for (auto a = primes.begin (); a < primes.end (); a++) {
int counter = 1;
int most = 0;
int s = *a;
int p = 0;
for (auto b = a + 1; b < primes.end (); b++) {
s += *b;
counter++;
if (s < 1000000) {
if (isPrime (s)) {
most = counter;
p = s;
}
} else {
break;
}
}
if (most > mostOverall) {
mostOverall = most;
pOverall = p;
}
}
cout << mostOverall << endl;
cout << pOverall << endl;
}