-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectEuler049.cpp
More file actions
126 lines (92 loc) · 2.56 KB
/
Copy pathprojectEuler049.cpp
File metadata and controls
126 lines (92 loc) · 2.56 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* Problem 49
Prime Permutations
The arithmetic sequence, 1487, 4817, 8147, in which each of the terms
increases by 3330, is unusual in two ways: (i) each of the three terms
are prime, and, (ii) each of the 4-digit numbers are permutations of
one another.
There are no arithmetic sequences made up of three 1-, 2-, or 3-digit
primes, exhibiting this property, but there is one other 4-digit
increasing sequence.
What 12-digit number do you form by concatenating the three terms
in this sequence?
*/
#include <algorithm>
#include <iostream>
#include <string>
#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 = 1000; i < 10000; i++) {
if (isPrime (i)) {
primes.push_back (i);
}
}
return primes;
}
int strToInt (string s) {
int result = 0;
for (int i = 0; i < s.length (); i++) {
result += int (s [i] - 48) * pow (10, s.length () - 1 - i);
}
return result;
}
int main () {
vector <int> primes = getPrimes ();
bool found = false;
for (auto a = primes.begin (); a < primes.end (); a++) {
int digits [4] = { };
string s = to_string (*a);
for (int a = 0; a < 4; a++) {
digits [a] = int (s [a] - 48);
}
vector <int> permutations;
permutations.push_back (*a);
sort (digits, digits + 4);
do {
s = "";
for (int a = 0; a < 4; a++) {
s += to_string (digits [a]);
}
int perm = strToInt (s);
if (isPrime (perm)) {
if (find (permutations.begin (), permutations.end (), perm) == permutations.end ()) {
permutations.push_back (perm);
}
}
} while (next_permutation (digits, digits + 4));
if (permutations.size () >= 3) {
for (int i = 1; i < 5000; i++) {
int candA = permutations [0] + i;
int candB = permutations [0] + (2 * i);
if (find (permutations.begin (), permutations.end (), candA) != permutations.end ()) {
if (find (permutations.begin (), permutations.end (), candB) != permutations.end ()) {
cout << permutations [0] << " ";
cout << candA << " ";
cout << candB << " ";
cout << endl;
break;
}
}
}
}
if (found) {
break;
}
}
}