-
Notifications
You must be signed in to change notification settings - Fork 0
/
10235.cpp
66 lines (57 loc) · 1.44 KB
/
10235.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
#include <cmath>
#include <cstdio>
using namespace std;
int main(void)
{
bool check[1000000], pflag, eflag;
int len, list[1000000], i, j, n, rn, beg, end, tmpInt;
for (i = 2; i < 1000000; ++i)
check[i] = true;
for (i = 2; i < 1000000; ++i)
if (check[i])
for (j = 2 * i; j < 1000000; j += i)
check[j] = false;
len = 0;
for (i = 2; i < 1000000; ++i)
if (check[i]) list[len++] = i;
while (scanf("%d", &n) != EOF) {
beg = 0;
end = len;
while (end - beg > 1) {
i = (beg + end) / 2;
if (list[i] > n)
end = i;
else if (list[i] < n)
beg = i;
else
break;
}
pflag = list[i] == n;
tmpInt = n;
rn = 0;
while (tmpInt > 0) {
rn *= 10;
rn += tmpInt % 10;
tmpInt /= 10;
}
beg = 0;
end = len;
while (pflag && end - beg > 1) {
i = (beg + end) / 2;
if (list[i] > rn)
end = i;
else if (list[i] < rn)
beg = i;
else
break;
}
eflag = n != rn && list[i] == rn;
if (!pflag)
printf("%d is not prime.\n", n);
else if (!eflag)
printf("%d is prime.\n", n);
else
printf("%d is emirp.\n", n);
}
return 0;
}