-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path10006.cpp
executable file
·117 lines (100 loc) · 1.89 KB
/
10006.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <vector>
using namespace std ;
#define MaxPrime 65000
int previous[ MaxPrime + 1 ] = { 0 } ;
int next[ MaxPrime + 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 pow( long long int b, long long int p, long long int m )
{
long long int temp = 0 ;
if( p == 0 )
{
return 1 ;
}
else if( p == 1 )
{
return b % m ;
}
else
{
temp = pow( b, p / 2, m ) ;
if( p % 2 == 0 )
{
return ( temp * temp ) % m ;
}
else
{
return ( ( temp * temp ) % m * b ) % m ;
}
}
}
bool fermat_test( int n )
{
int a = 0 ;
int temp = 0 ;
int i = 0 ;
for( a = 2 ; a <= n-1 ; a++ )
{
if( pow( a, n, n ) != a )
return false ;
}
return true ;
}
int main()
{
int n = 0 ;
int i = 0 ;
cin.sync_with_stdio( false ) ;
cout.sync_with_stdio( false ) ;
cal( MaxPrime ) ;
while( cin >> n && n != 0 )
{
if( next[ n ] == 0 && fermat_test( n ) == true )
{
cout << "The number " << n << " is a Carmichael number." << endl ;
}
else
{
cout << n << " is normal." << endl ;
}
}
return 0 ;
}