-
Notifications
You must be signed in to change notification settings - Fork 0
/
10235 - Simply Emirp.c
84 lines (71 loc) · 1.48 KB
/
10235 - Simply Emirp.c
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
#include<stdio.h>
#include<string.h>
#include<math.h>
#define MAX 1000000
int p[MAX];
void sieveMethod()
{
memset(p, 0, sizeof(p));
p[0] = p[1] = 1;
int i, j;
for(i = 4; i < MAX; i += 2)
{
p[i] = 1;
}
int sq = sqrt(MAX);
for(i = 3; i < sq; i+=2)
{
if(p[i] == 1) continue;
for(j = i*i; j < MAX; j += 2*i)
{
p[j] = 1;
}
}
}
int lengthOfNumber(int num)
{
int count = 0;
while(num > 0)
{
count++;
num /= 10;
}
return count;
}
int reverse (int num)
{
int len = lengthOfNumber(num), reversedNumber = 0;
int div = 10, mul;
double pw = (double)(len - 1);
pw = pow(10.0, pw);
mul = pw;
while(len--)
{
int rem = num % 10;
reversedNumber += (rem * mul);
num /= 10;
mul /= 10;
}
return reversedNumber;
}
int main (void)
{
sieveMethod();
long long int N;
while(scanf("%lld", &N) == 1)
{
long long int t = reverse(N);
if(t == N)
{
if(p[N] == 0) printf("%lld is prime.\n", N);
else printf("%lld is not prime.\n", N);
}
else
{
if(p[N] == 0 && p[t] == 0) printf("%lld is emirp.\n", N);
else if(p[N] == 0) printf("%lld is prime.\n", N);
else printf("%lld is not prime.\n", N);
}
}
return 0;
}