Carmichael Numbers

December 28, 2010

Pierre de Fermat’s little theorem states that if p is prime, then for any integer a, apa is evenly divisible by p, which is usually stated as apa (mod p); Fermat’s little theorem is often stated as the corollary ap−1 ≡ 1 (mod p).

By turning it around, Fermat’s little theorem may be used as a primality test: if you can find an a for which ap−1 ≢ 1 (mod p), then p is certainly composite; for instance, 262 ≡ 4 (mod 63), so 63 is composite, and 2 is a witness to its compositeness. Thus Fermat’s primality test consists of choosing many numbers and checking if they are witnesses to the compositeness of the number being tested.

Unfortunately, there are some composite numbers which pass Fermat’s primality test for all possible witnesses; they are called Carmichael numbers, after Robert Carmichael, who studied them. Carmichael discovered in 1910 that 561 is a Fermat pseudo-prime to all possible bases.

One way to test that a number is a Carmichael number is to test all primes less than the number that are coprime to it. A faster test, due to Alwin Korselt, notes that Carmichael numbers are odd, composite, square-free (no factors appear more than once) and f−1 ∣ n−1 for all factors f of n.

Because there exist numbers that fool Fermat’s primality test for all bases, a strong pseudo-primality test is often used. A composite number n = d · 2s + 1 with d odd is a strong pseudoprime to a relatively prime base a if and only if either ad ≡ 1 (mod n) or ad·2r ≡ −1 (mod n) for some 0 ≤ rs−1. For instance, 231 ≡ 2 (mod 63), so 63 = 31 · 21 + 1 is composite, and 2 is a witness to that compositeness. The beauty of the strong pseudo-primality test is that it harbors no “Carmichael numbers;” every composite has at least one strong witness.

Your tasks are to write two functions that test if a number is a Fermat pseudo-prime or a strong pseudo-prime to a given base, two functions that test primality using the Fermat and strong pseudo-prime tests, and two functions that test if a number is a Carmichael number, and to identify all the Carmichael numbers less than 10,000. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.


Pages: 1 2

One Response to “Carmichael Numbers”

  1. Graham said

    This is where the criticism of Python being too slow gains ground, in my
    opinion. Trying to do number intensive stuff takes a while. My solution
    has a few changes: I went with probabilistic testing instead of deterministic
    testing, had my _factors(n) give just primes, etc. The logic is
    mostly the same.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: