-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem49.cpp
74 lines (60 loc) · 1.23 KB
/
problem49.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
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
using namespace std;
#define N 1e6
vector<bool> isPrime(N+1,true);
void sieve()
{
isPrime[1] = true;
isPrime[2] = true;
for (int i = 2; i <= sqrt(N);i++){
for (int j = i*i; j<=N;j+=i){
isPrime[j] = false;
}
}
}
vector<int> vectorize(int a){
vector<int> digits;
while (a){
digits.push_back(a%10);
a /= 10;
}
return digits;
}
bool verifyPerms(int a, int b, int c)
{
map<int, bool> hist;
auto digitsA = vectorize(a);
auto digitsB = vectorize(b);
auto digitsC = vectorize(c);
if (digitsA.size() != digitsB.size() || digitsA.size() != digitsC.size() ||
digitsB.size() != digitsC.size()) return false;
for (auto d : digitsA){hist[d] = true;}
for (auto d : digitsB){if (!hist[d]) return false;}
for (auto d : digitsC){if (!hist[d]) return false;}
return true;
}
bool verify(int a, int b, int c)
{
bool allPrimes = isPrime[a] && isPrime[b] && isPrime[c];
bool allPerms = verifyPerms(a,b,c);
return allPrimes && allPerms;
}
int main()
{
sieve();
int x = 1488;
int inc = 3330;
while (true){
int a = x;
int b = a + inc;
int c = b + inc;
if (verify(a,b,c)){
cout<<a<<"-"<<b<<"-"<<c;
break;
}
x++;
}
}