TExcelTAD
Home › Number Theory & Cryptography › Carmichael Numbers

Carmichael Numbers

Some composite numbers satisfy Fermat's congruence an−1≡1a^{n-1} \equiv 1 for every base aa 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 nn is a Carmichael number if an−1≡1(modn)a^{n-1} \equiv 1 \pmod{n} for all aa with gcd⁡(a,n)=1\gcd(a, n) = 1. The smallest is 561=3⋅11⋅17561 = 3 \cdot 11 \cdot 17.

python · try it yourself
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 nn is a Carmichael number if and only if nn is squarefree and p−1∣n−1p - 1 \mid n - 1 for every prime p∣np \mid n.

Why it suffices: if p−1∣n−1p - 1 \mid n - 1, then for aa coprime to pp, Fermat gives an−1=(ap−1)(n−1)/(p−1)≡1(modp)a^{n-1} = (a^{p-1})^{(n-1)/(p-1)} \equiv 1 \pmod{p}. This holds for every prime factor, and since nn is squarefree the CRT combines them into an−1≡1(modn)a^{n-1} \equiv 1 \pmod{n}.

For 561: n−1=560n - 1 = 560, and 2∣5602 \mid 560, 10∣56010 \mid 560, 16∣56016 \mid 560.

python · try it yourself
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 6k+16k + 1, 12k+112k + 1 and 18k+118k + 1 are all prime, their product is a Carmichael number: n−1n - 1 is divisible by 36k36k, which is a multiple of 6k6k, 12k12k and 18k18k. With k=1k = 1: 7⋅13⋅19=17297 \cdot 13 \cdot 19 = 1729, the Hardy-Ramanujan taxicab number.

python · try it yourself
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, x2≡1x^2 \equiv 1 has only the solutions x≡±1x \equiv \pm 1 (a field has no zero divisors: (x−1)(x+1)=0(x - 1)(x + 1) = 0). Modulo 561 there are more:

python · try it yourself
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 −1≡560-1 \equiv 560. 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 1105=5⋅13⋅171105 = 5 \cdot 13 \cdot 17 is a Carmichael number.

Answer

11041104 is divisible by 4, 12 and 16.

Solution

1104=4⋅276=12⋅92=16⋅691104 = 4 \cdot 276 = 12 \cdot 92 = 16 \cdot 69. It is squarefree with three prime factors, so Korselt's criterion applies.

Exercise 2: Not Carmichael

Why is 15=3⋅515 = 3 \cdot 5 not a Carmichael number? Why can no product of two distinct primes be one?

Answer

4∤144 \nmid 14. For n=pqn = pq with p<qp < q, q−1∣pq−1q - 1 \mid pq - 1 would force q−1∣p−1q - 1 \mid p - 1.

Solution

For 15, 5−1=45 - 1 = 4 does not divide 14. In general pq−1=p(q−1)+(p−1)pq - 1 = p(q - 1) + (p - 1), so q−1∣pq−1q - 1 \mid pq - 1 implies q−1∣p−1q - 1 \mid p - 1, impossible since 0<p−1<q−10 < p - 1 < q - 1.

Exercise 3: A non-trivial square root (code)

Find the non-trivial square root of 1 that base 5 reveals for 1729.

Answer

Square 5275^{27} repeatedly (1728=26⋅271728 = 2^6 \cdot 27) and take the value just before the first 1.

Solution
python · try it yourself
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 yy of 1 also gives factors: gcd⁡(y−1,n)\gcd(y - 1, n) and gcd⁡(y+1,n)\gcd(y + 1, n).

Test yourself

The smallest Carmichael number is…

  1. 341
  2. 561
  3. 1729
  4. 91

Korselt's criterion: a squarefree composite nn is Carmichael iff for each prime p∣np \mid n…

  1. p∣n−1p \mid n - 1
  2. p−1∣n−1p - 1 \mid n - 1
  3. p+1∣np + 1 \mid n
  4. p2∣np^2 \mid n

Modulo a prime pp, the solutions of x2≡1x^2 \equiv 1 are…

  1. only 1
  2. ±1\pm 1 only
  3. four values
  4. all xx