Tonelli-Shanks algorithm Created 2026-10-20 Updated 2026-11-05
The Tonelli-Shanks algorithm finds a square root of a quadratic residue modulo an odd prime number . Handle separately and use the Legendre symbol to reject a quadratic nonresidue. Write with odd, choose a quadratic nonresidue , and initialize
modulo . Maintain , with of multiplicative order and of multiplicative order dividing .
If , find the least with . Set and update
The multiplicative order of decreases because its last nontrivial power and that of are both minus one. Thus strictly decreases. At , the invariant gives .
For , initialization gives . One update gives , so the roots are and modulo thirteen.[ref]