An important topic nowadays in computer science is cryptography. Some people even
that cryptography is the only important field in computer science, and that
life would not matter at all without cryptography.
Alvaro is one of such persons, and is designing a set of cryptographic
procedures for cooking paella. Some of the cryptographic algorithms he is
implementing make use of big prime numbers. However, checking if a big number
is prime is not so easy. An exhaustive approach can require the division of
the number by all the prime numbers smaller or equal than its square root. For
big numbers, the amount of time and storage needed for such operations would
certainly ruin the paella.
However, some probabilistic tests exist that offer
high confidence at low cost. One of them is the Fermat test.
Let a be a random number between 2 and n - 1 (being n the number whose primality we are testing). Then, n is probably prime if the following equation holds:
If a number passes the Fermat test several times then it is prime with a high probability. Unfortunately, there are bad news. Some numbers that are not prime still pass the Fermat test with every number smaller than themselves. These numbers are called Carmichael numbers.
In this problem you are asked to write a program to test if a given number is a Carmichael number. Hopefully, the teams that fulfill the task will one day be able to taste a delicious portion of encrypted paella. As a side note, we need to mention that, according to Alvaro, the main advantage of encrypted paella over conventional paella is that nobody but you knows what you are eating.
InputThe input will consist of a series of lines, each containing a small positive number n ( 2 < n < 65000). A number n = 0 will mark the end of the input, and must not be processed.
OutputFor each number in the input, you have to print if it is a Carmichael number or not, as shown in the sample output.
1729 17 561 1109 431 0
The number 1729 is a Carmichael number. 17 is normal. The number 561 is a Carmichael number. 1109 is normal. 431 is normal.
Solution:First, for every a ranged from 2 to n-1, check if a^n mod n == a.
Then, if it's also not a prime number, Bingo.
PS: using unsigned long will cause a lot of trouble, since we need to multiply two large numbers, that will cause overflow.
Source Code://Mon Apr 4 01:15:58 CDT 2011
using namespace std;
const long maxn = 65007;
long Prime[maxn], tot;
for (long i = 1; i < maxn; i++) isPrime[i] = true;
isPrime = false;
tot = 0;
for (long i = 2; i < maxn; i++)
Prime[tot] = i;
for (long j = 1; j <= tot && i * Prime[j] < maxn; j++)
isPrime[i * Prime[j]] = false;
if (i % Prime[j] == 0) break;
long modN(long a, long m, long n)
if (m == 1)
return a % n;
if (m % 2 == 0)
long tmp = modN(a, m / 2, n) % n;
return (tmp * tmp) % n;
long tmp = modN(a, (m - 1) / 2, n) % n;
tmp = (tmp * tmp) % n;
return a * tmp % n;
bool isCarmichael(long n)
for (long i = 2; i < n; i++)
if (modN(i, n, n) != i)
int main(int argc, char* argv)
//freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
while (cin >> n && n)
if (!isPrime[n] && isCarmichael(n))
cout << "The number " << n << " is a Carmichael number." << endl;
cout << n << " is normal." << endl;