-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCarmichaelNumbers.cpp
56 lines (52 loc) · 1011 Bytes
/
CarmichaelNumbers.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
#include<iostream>
#include<cmath>
#include<stdlib.h>
#define M 65001
using namespace std;
bool prime[M];
int genprime();
bool fermat(long int);
int primog(){
unsigned long i,j,s=sqrt(M);
for(i=2;i<M;i++)
prime[i]=true;
for(i=2;i<s;){
if(prime[i])
for(j=i+i;j<M;j+=i)
prime[j]=false;
for(i++;!prime[i];i++);
}
return 0;
}
bool fermat(long int n)
{
long int a,m,z,y;
for(a=2;a<n;a++){
m=n;
y=1;
z=a;
while(m>0){
while(m%2==0){
z=(z*z)%n;
m>>=1;
}
m--;
y=(y*z)%n;
}
if(y!=a)return false;
}
return true;
}
int main(){
long int n;
primog();
while(cin>>n&&n){
if(prime[n])
cout<<n<<" is normal.\n";
else if(fermat(n))
cout<<"The number "<<n<<" is a Carmichael number.\n";
else
cout<<n<<" is normal.\n";
}
exit(0);
}