The Fermat Primality Test
Trial division cannot test a 300-digit number. Fermat's little theorem gives a test that runs in milliseconds: if , then is certainly composite. If the congruence holds for several bases, is probably prime.
The test
By Fermat's Little Theorem, a prime satisfies for all coprime to . Read contrapositively:
- If for some with , then is composite, and is a Fermat witness. We learn is composite without finding a factor.
- If , then is a probable prime to base . A composite that passes is a Fermat pseudoprime to base , and is a Fermat liar.
def fermat_test(n, bases=(2, 3, 5, 7)):
for a in bases:
if a % n and pow(a, n - 1, n) != 1:
return False, a # a is a witness: n is composite
return True, None
for n in (561, 1001, 1_000_003, 2**61 - 1, 2**61 + 1, 2**89 - 1):
print(n, fermat_test(n))561 (False, 3)
1001 (False, 2)
1000003 (True, None)
2305843009213693951 (True, None)
2305843009213693953 (False, 2)
618970019642690137449562111 (True, None)
The 1001 case shows a witness found immediately. 561 fools every base listed except bases sharing a factor with it, a Carmichael number (next lesson).
How often does it lie?
For base 2, pseudoprimes exist but are rare: the smallest is .
from sympy import isprime
psp2 = [n for n in range(3, 100000, 2) if not isprime(n) and pow(2, n - 1, n) == 1]
print(len(psp2), psp2[:10])
both = [n for n in psp2 if pow(3, n - 1, n) == 1]
print("also pass base 3:", len(both), both[:8])78 [341, 561, 645, 1105, 1387, 1729, 1905, 2047, 2465, 2701]
also pass base 3: 23 [1105, 1729, 2465, 2701, 2821, 6601, 8911, 10585]
Below 100000 there are 78 base-2 pseudoprimes, against 9592 primes, and most fail base 3 as well.
Liars are at most half (usually)
For a composite , the Fermat liars form a subgroup of the units. If there is even one witness coprime to , the subgroup is proper, so at most half the units are liars. Then random bases miss with probability at most .
from math import gcd
def liar_fraction(n):
units = [a for a in range(1, n) if gcd(a, n) == 1]
liars = [a for a in units if pow(a, n - 1, n) == 1]
return len(liars), len(units)
for n in (341, 91, 1001, 561):
l, u = liar_fraction(n)
print(f"n={n:5} liars {l:4} of {u:4} units ({l / u:.1%})")n= 341 liars 100 of 300 units (33.3%)
n= 91 liars 36 of 72 units (50.0%)
n= 1001 liars 80 of 720 units (11.1%)
n= 561 liars 320 of 320 units (100.0%)
The exception is 561: every unit is a liar. Numbers like that break the "at most half" argument, which is why practical code uses Miller-Rabin instead.
In software
- PGP and some older libraries used Fermat tests with a few small bases to generate keys; they were acceptable because random large numbers are very unlikely to be Carmichael numbers.
- The test is still used as a cheap pre-filter. GMP's
mpz_probab_prime_pruns trial division, then a base-210 Fermat-style check, then Miller-Rabin rounds.
Exercises
Exercise 1: A witness by hand
Show that 2 is a Fermat witness for .
Answer
.
Solution
, so . Hence 15 is composite, which we knew, but the test did not need a factor.
Exercise 2: 341
Verify that although .
Answer
, and .
Solution
Since , . Base 3 exposes it: (check with pow(3, 340, 341)).
Exercise 3: First pseudoprimes (code)
Find the smallest odd Fermat pseudoprime to base 3 and to base 5.
Answer
91 for base 3 and 217 for base 5.
Solution
from sympy import isprime
for a in (3, 5):
print(a, next(n for n in range(3, 10**6, 2) if not isprime(n) and pow(a, n - 1, n) == 1))3 91
5 217
and . Each base has its own pseudoprimes, so combining bases helps, except against Carmichael numbers.
Test yourself
If , then…
- is prime
- is composite
- is prime
- nothing can be concluded
The smallest base-2 Fermat pseudoprime is…
- 91
- 341
- 561
- 1105
Why can a Fermat test with many bases still fail?
- exponentiation is inexact
- Carmichael numbers pass for every base coprime to them
- large primes fail it
- it needs factoring