Problem Description
Problem Analysis
The conditions for a number to be a Carmichael Number are
- It is not prime (the number is composite)
- For all x in [2,n-1], xn≡x(mod n)
Use a sieve to find primes
Use fast modular exponentiation to compute powers
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
| #include<stdio.h>
#include<string.h>
int prime[65200];
void getprime() {
memset(prime,0,sizeof(prime));
prime[0]=prime[1]=1;
for(int i=2; i<30000; i++)
if(!prime[i]) {
for(int j=i*i; j<65200; j+=i)
prime[j]=1;
}
return ;
}
int pow(int a,int b) {
int MOD=b;
long long r=1,t=a%MOD;
while(b) {
if(b&1)
r=r*t%MOD;
t=t*t%MOD;
b>>=1;
}
return (int)r;
}
bool judge(int n) {
if(!prime[n])
return false;
for(int i=2; i<n; i++)
if(pow(i,n)!=i) {
return false;
}
return true;
}
int main() {
getprime();
int n;
while(scanf("%d",&n),n) {
if(judge(n))
printf("The number %d is a Carmichael number.\n",n);
else
printf("%d is normal.\n",n);
}
return 0;
}
|