-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEP49.cpp
More file actions
66 lines (59 loc) · 1.55 KB
/
EP49.cpp
File metadata and controls
66 lines (59 loc) · 1.55 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
#include <stdio.h>
#include <algorithm>
#include <inttypes.h>
#define MAX_N 10000
typedef struct {
int32_t num;
int32_t rnum;
} sInt;
int32_t prime[MAX_N + 5] = {0};
int32_t pSint[MAX_N + 5] = {0};
int32_t dLen = 0;
sInt d[MAX_N + 5] = {0};
int32_t IntToSint(int32_t n) {
int32_t rnum = 0, x = n;
while (n) {
rnum += 1 << (2 * (n % 10));
n /= 10;
}
return rnum;
}
bool cmp(sInt a, sInt b) {
if (a.rnum < b.rnum) return true;
if (a.rnum == b.rnum && a.num < b.num) return true;
return false;
}
void InitData() {
for (int32_t i = 2; i <= MAX_N; i++) {
if (!prime[i]) {
prime[++prime[0]] = i;
if (i >= 1000) {
d[dLen].num = i;
d[dLen].rnum = IntToSint(i);
pSint[i] = d[dLen].rnum;
dLen++;
}
}
for (int32_t j = 1; j <= prime[0]; j++) {
if (i * prime[j] > MAX_N) break;
prime[i * prime[j]] = 1;
if (i % prime[j] == 0) break;
}
}
}
int32_t main() {
InitData();
std::sort(d, d + dLen, cmp);
for (int32_t i = 0; i < dLen - 2; i++) {
int32_t j = i + 1, nextNum;
while (d[j].rnum == d[i].rnum) {
nextNum = 2 * d[j].num - d[i].num;
if (nextNum >= 10000) break;
if (pSint[nextNum] == d[i].rnum) {
printf("%d%d%d\n", d[i].num, d[j].num, nextNum);
}
j++;
}
}
return 0;
}