-
Notifications
You must be signed in to change notification settings - Fork 0
/
049.cpp
106 lines (90 loc) · 2.03 KB
/
049.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
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
#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;
}
bool couldBePerm(int x, int y) {
return (y - x) % 9 == 0;
}
bool isIn(int n, std::vector<int> array) {
unsigned long length = array.size();
for (int i = 0; i < length && n <= array[length - 1]; i++) {
if (array[i] == n) return true;
}
return false;
}
int sumDigits(int n) {
int digit;
int sum = 0;
while (n) {
digit = n % 10;
sum += digit;
n /= 10;
}
return sum;
}
bool remove(int digit, std::vector<int> &digits) {
for (int i = 0; i < digits.size(); i++) {
if (digits[i] == digit) {
digits.erase(digits.begin() + i);
return true;
}
}
return false;
}
bool isPerm(int x, int y, int z) {
int digit;
std::vector<int> digits1;
std::vector<int> digits2;
for (int i = 0; i < 4; i++) {
digit = x % 10;
digits1.push_back(digit);
digits2.push_back(digit);
x /= 10;
}
for (int i = 0; i < 4; i++) {
digit = y % 10;
if (!remove(digit, digits1)) return false;
y /= 10;
}
for (int i = 0; i < 4; i++) {
digit = z % 10;
if (!remove(digit, digits2)) return false;
z /= 10;
}
return true;
}
int main() {
std::vector<int> primes;
for (int n = 1000; n < 9999; n++) {
if (isPrime(n)) {
primes.push_back(n);
}
}
int diff, target;
for (int i = 0; i < primes.size(); i++) {
for (int j = i + 1; j < primes.size(); j++) {
if (couldBePerm(primes[i], primes[j])) {
diff = primes[j] - primes[i];
target = primes[j] + diff;
if (target > primes[primes.size() - 1]) continue;
if (
isIn(target, primes) &&
isPerm(primes[i], primes[j], target)
) {
std::cout << primes[i] << " " << primes[j] << " " << target <<std::endl;
}
}
}
}
return 0;
}