Maths WorkshopMaths problems with full solutions
Exercise 2203Number theory › PrimesM1Added 3 Nov 2026

The Miller–Rabin test

#Miller-Rabin#primality#probabilistic

Statement

Describe the Miller–Rabin test and apply it to n=13n=13 with base a=2a=2.

Hint

Write n−1=2s⋅dn-1=2^s\cdot d with dd odd. Compute ad mod na^d\bmod n, then square up to ss times.

Show solutionHide solution

Solution

Setup. n=13n=13, n−1=12=22⋅3n-1=12=2^2\cdot3, so s=2s=2, d=3d=3.

Compute. ad=23=8(mod13)a^d=2^3=8\pmod{13}. Square: 82=64≡12≡−1(mod13)8^2=64\equiv12\equiv-1\pmod{13}. Witness for primality: we hit −1-1 before squaring s=2s=2 times.

Conclusion. 1313 passes Miller–Rabin for base 22.

Application. The test is used in RSA key generation; with kk bases, error probability ≤4−k\le4^{-k} for composite inputs.