-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path037.cpp
63 lines (50 loc) · 881 Bytes
/
037.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
#include <iostream>
#include <math.h>
using namespace std;
typedef unsigned long long ULL;
ULL reverse(ULL a) {
ULL result = 0;
while(a > 0) {
result = result * 10 + a % 10;
a /= 10;
}
return result;
}
bool isPrime(ULL a) {
if(a == 1) return false;
ULL maxFrac = pow(a, 0.5);
for(int i = 2; i <= maxFrac; ++i) {
if(a % i == 0) return false;
}
return true;
}
ULL nextLeft(ULL a) {
return reverse(reverse(a) / 10);
}
bool hasPropertyLeft(ULL a) {
while(a > 0) {
if(!isPrime(a)) return false;
a = nextLeft(a);
}
return true;
}
bool hasPropertyRight(ULL a) {
while(a > 0) {
if(!isPrime(a)) return false;
a /= 10;
}
return true;
}
int main() {
ULL sum = 0;
int k = 11;
for(ULL i = 11; k > 0; ++i) {
if(hasPropertyLeft(i) && hasPropertyRight(i)) {
sum += i;
cout << i << endl;
k--;
}
}
cout << "Result: " << sum;
return 0;
}