Exercise 2123Number theory › Divisibility and gcdM1Added 3 Nov 2026
The Miller–Rabin test
#primality testing#Miller-Rabin#modular arithmetic
Statement
Describe the Miller–Rabin primality test for odd , writing with odd.
Hint
For a base , compute , then square repeatedly up to times.
Show solutionHide solution
Solution
Setup. Write with odd. Choose a base with .
Test. Compute . Then square times:
- if or , the base is a witness for primality;
- otherwise, if after squaring times we reach neither nor , the base is a witness for compositeness.
Correctness. If is prime, Fermat's little theorem plus the structure of ensures every coprime base is a witness for primality. If is composite, at least of bases are witnesses for compositeness (for odd composite ).
Conclusion. Miller–Rabin is a probabilistic test with error probability for bases.