On this page
Trial division is hopeless for a number hundreds of digits long. The Miller–Rabin test needs only repeated squaring, and you can run it on small numbers with a pencil.
Why isn't Fermat's little theorem enough?
Fermat's little theorem says that if n is prime, then aⁿ⁻¹ ≡ 1 (mod n) for every a from 1 to n − 1. The trouble is that some composites pass for every base coprime to them. These are the Carmichael numbers, and the smallest is 561, which is 3 × 11 × 17. Since 2⁵⁶⁰ ≡ 1 (mod 561), Fermat's test is fooled. Miller–Rabin catches 561 by checking the steps on the way to 2⁵⁶⁰, not just the end.
How does the Miller–Rabin test work?
The key fact: modulo a prime, the only square roots of 1 are 1 and −1. Gary Miller published a deterministic version resting on an unproved hypothesis in 1976 (Riemann's hypothesis and tests for primality80043-8)), and Michael Rabin made it a random test in 1980 (Probabilistic algorithm for testing primality90084-0)). The steps, following Keith Conrad's notes on the test:
For odd n, write n − 1 = 2ˢd with d odd.
Pick a base a from 2 to n − 2.
Compute aᵈ mod n, then square it again and again: aᵈ, a²ᵈ, a⁴ᵈ, and so on, s terms in all.
n passes for this base if the first term is 1, or if −1 (that is, n − 1) appears anywhere in the chain.
Otherwise a is a witness and n is definitely composite.
For a prime, squaring the last term gives aⁿ⁻¹ ≡ 1, so the first 1 to appear is either the first term or the square of a −1.
Worked example: is 97 prime?
Write 96 = 2⁵ × 3, so s = 5 and d = 3. Take a = 2.
2³ = 8
8² = 64
64² = 4096 = 42 × 97 + 22, so 22
22² = 484 = 5 × 97 − 1, so −1 (written 96)
(−1)² = 1
The chain is 8, 64, 22, 96, 1. It hits −1, so 97 passes for base 2. Base 5 gives 28, 8, 64, 22, 96 and passes too. Passing never proves primality; it is evidence.
Worked example: why does 561 fail?
Write 560 = 2⁴ × 35, so s = 4 and d = 35. With a = 2, using 2¹⁰ ≡ 463 and 2²⁰ ≡ 67 (mod 561), you get 2³⁵ ≡ 263. Now square:
263² = 69169 ≡ 166
166² = 27556 ≡ 67
67² = 4489 = 8 × 561 + 1, so 1
The chain 263, 166, 67, 1 does not start at 1 and never shows 560. So 2 is a witness and 561 is composite, even though Fermat's test passed it.
Try it yourself on 341: write 340 = 2² × 85, and since 2¹⁰ = 1024 = 3 × 341 + 1, the chain for base 2 is 32, 1. That makes 2 a witness, so 341 is composite (it is 11 × 31). Since one slip in a reduction spoils the chain, the Fermi Problem Library helps: its step-by-step solver explains the reasoning.
Why does a composite fool fewer than a quarter of bases?
For every odd composite n, more than 75% of the integers from 2 to n − 2 are Miller–Rabin witnesses, a theorem proved independently by Monier and Rabin, according to Conrad's notes. So each random base catches a composite with chance above 3/4, and t independent bases all miss it with chance below 1/4ᵗ.
An easier bound comes from Lagrange's theorem on subgroup orders: for a composite that is not a Carmichael number, the bases that fool Fermat's test form a proper subgroup, so they are fewer than half. Miller–Rabin also catches fake square roots of 1, and a harder proof gets its bound down to a quarter. If modular arithmetic feels shaky, the Fermi Concept Library covers the ideas underneath.
How is Miller–Rabin used to pick RSA primes?
RSA needs two large secret primes, p and q. Primes are common enough, as the prime number theorem shows, that software picks random odd numbers of the right size and tests each until one passes.
The US signature standard NIST FIPS 186-5 (2023) sets the rules. Section B.3 allows optional trial division by small primes first, then Miller–Rabin rounds, optionally followed by one Lucas test. Its Table B.1 asks for at least 4 rounds for 1024-bit p and q at an error probability of 2⁻¹⁰⁰, and 5 rounds at 2⁻¹¹². The worst-case 1/4 bound would need 50 rounds for 2⁻¹⁰⁰. Appendix C measures something else: the chance that a randomly chosen odd number which passes is still composite, which is far smaller.
A plan for practising: run the chain on 97, 561 and 341 by hand, then write a short program with fast modular powers and test 1373653, which Conrad's notes give as the first odd composite that fools both bases 2 and 3.



