-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path10235.cpp
executable file
·101 lines (84 loc) · 1.51 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <iomanip>
using namespace std ;
#define Max_n 1000000
int previous[ Max_n + 1 ] = { 0 } ;
int next[ Max_n + 1 ] = { 0 } ;
void initial( long long int n )
{
long long int i = 0 ; // loop counter
previous[ 2 ] = -1 ;
next[ 2 ] = 3 ;
previous[ 3 ] = 2 ;
next[ 3 ] = 5 ;
for( i = 3 ; i <= n ; i += 2 )
{
previous[ i ] = i - 2 ;
next[ i ] = i + 2 ;
}
}
void remove( int x )
{
next[ previous[ x ] ] = next[ x ] ;
previous[ next[ x ] ] = previous[ x ] ;
previous[ x ] = 0 ;
next[ x ] = 0 ;
}
void cal( long long int n )
{
long long int numPrimes = 0 ;
long long int prime = 0 ;
long long int fact = 0 ;
long long int mult = 0 ;
initial( n ) ;
for( prime = 3 ; prime * prime <= n ; prime = next[ prime ] )
{
for( fact = prime ; prime * fact <= n ; fact = next[ fact ] )
{
for( mult = prime * fact ; mult <= n ; mult *= prime )
{
remove( mult ) ;
}
}
}
}
long long int reverse( int n )
{
long long int value = 0 ;
value = n % 10 ;
while( n >= 10 )
{
n /= 10 ;
value = value * 10 + n % 10 ;
}
return value ;
}
int
main()
{
int n = 0 ;
int reverseN = 0 ;
int i = 0 ; // loop counter
int j = 0 ; // loop counter
cal( Max_n ) ;
while( cin >> n )
{
reverseN = reverse( n ) ;
if( next[ n ] != 0 )
{
if( n != reverseN && next[ reverseN ] != 0 )
{
cout << n << " is emirp." << endl ;
}
else
{
cout << n << " is prime." << endl ;
}
}
else
{
cout << n << " is not prime." << endl ;
}
}
return 0 ;
}