Carmichael Numbers
Some composite numbers satisfy Fermat's congruence for every base coprime to them. These Carmichael numbers defeat the Fermat test completely, and studying why leads straight to the stronger Miller-Rabin test.
Definition and the smallest example
A composite is a Carmichael number if for all with . The smallest is .
from math import gcd
n = 561
units = [a for a in range(1, n) if gcd(a, n) == 1]
print(len(units), all(pow(a, n - 1, n) == 1 for a in units))
print("bases sharing a factor:", [a for a in range(2, 40) if gcd(a, n) > 1])320 True
bases sharing a factor: [3, 6, 9, 11, 12, 15, 17, 18, 21, 22, 24, 27, 30, 33, 34, 36, 39]
All 320 units are liars. Only bases sharing a factor with 561 expose it, and for a Carmichael number with large prime factors those are vanishingly rare.
Korselt's criterion
A composite is a Carmichael number if and only if is squarefree and for every prime .
Why it suffices: if , then for coprime to , Fermat gives . This holds for every prime factor, and since is squarefree the CRT combines them into .
For 561: , and , , .
from sympy import factorint
def is_carmichael(n):
f = factorint(n)
if len(f) < 2 or any(e > 1 for e in f.values()):
return False
return all((n - 1) % (p - 1) == 0 for p in f)
cs = [n for n in range(3, 100000, 2) if is_carmichael(n)]
print(len(cs), cs)
print({n: list(factorint(n)) for n in cs[:6]})16 [561, 1105, 1729, 2465, 2821, 6601, 8911, 10585, 15841, 29341, 41041, 46657, 52633, 62745, 63973, 75361]
{561: [3, 11, 17], 1105: [5, 13, 17], 1729: [7, 13, 19], 2465: [5, 17, 29], 2821: [7, 13, 31], 6601: [7, 23, 41]}
Korselt's criterion also shows every Carmichael number is odd and has at least three prime factors.
Chernick's construction
If , and are all prime, their product is a Carmichael number: is divisible by , which is a multiple of , and . With : , the Hardy-Ramanujan taxicab number.
from sympy import isprime
for k in range(1, 60):
a, b, c = 6 * k + 1, 12 * k + 1, 18 * k + 1
if isprime(a) and isprime(b) and isprime(c):
n = a * b * c
print(k, (a, b, c), n, all(pow(x, n - 1, n) == 1 for x in (2, 3, 5, 11)))1 (7, 13, 19) 1729 True
6 (37, 73, 109) 294409 True
35 (211, 421, 631) 56052361 True
45 (271, 541, 811) 118901521 True
51 (307, 613, 919) 172947529 True
55 (331, 661, 991) 216821881 True
56 (337, 673, 1009) 228842209 True
In 1994 Alford, Granville and Pomerance proved there are infinitely many Carmichael numbers, so no fixed list of exceptions can rescue the Fermat test.
The fix: square roots of 1
Modulo a prime, has only the solutions (a field has no zero divisors: ). Modulo 561 there are more:
n = 561
roots = [x for x in range(n) if x * x % n == 1]
print(len(roots), roots[:8])
x = pow(2, 35, n) # 560 = 2^4 * 35: square 2^35 four times
seq = [x]
for _ in range(4):
x = x * x % n
seq.append(x)
print("2^35, 2^70, 2^140, 2^280, 2^560 mod 561:", seq)8 [1, 67, 188, 254, 307, 373, 494, 560]
2^35, 2^70, 2^140, 2^280, 2^560 mod 561: [263, 166, 67, 1, 1]
The sequence reaches 1 through 67, a square root of 1 that is neither 1 nor . A prime could never do that, so 561 is composite. Turning this observation into a test gives Miller-Rabin.
In software
- A Fermat-only test would accept Carmichael numbers, and an attacker who supplies "primes" (for example Diffie-Hellman parameters in a protocol) can choose such numbers deliberately. A 2018 paper ("Prime and Prejudice") built composites that fooled several libraries' tests with adversarial inputs.
- Libraries therefore use Miller-Rabin with random bases, often plus a Lucas test (the Baillie-PSW combination), for any number that may come from an adversary.
Exercises
Exercise 1: Check Korselt
Verify that is a Carmichael number.
Answer
is divisible by 4, 12 and 16.
Solution
. It is squarefree with three prime factors, so Korselt's criterion applies.
Exercise 2: Not Carmichael
Why is not a Carmichael number? Why can no product of two distinct primes be one?
Answer
. For with , would force .
Solution
For 15, does not divide 14. In general , so implies , impossible since .
Exercise 3: A non-trivial square root (code)
Find the non-trivial square root of 1 that base 5 reveals for 1729.
Answer
Square repeatedly () and take the value just before the first 1.
Solution
from math import gcd
n, a = 1729, 5
x = pow(a, 27, n)
prev = None
while x != 1:
prev, x = x, x * x % n
print("root:", prev, " check:", prev * prev % n, " factors:", gcd(prev - 1, n), gcd(prev + 1, n))root: 1065 check: 1 factors: 133 13
A non-trivial square root of 1 also gives factors: and .
Test yourself
The smallest Carmichael number is…
- 341
- 561
- 1729
- 91
Korselt's criterion: a squarefree composite is Carmichael iff for each prime …
Modulo a prime , the solutions of are…
- only 1
- only
- four values
- all