On this page
Which whole number x leaves remainder 10 when x² is divided by 13? That is a square root modulo a prime, and it turns up in olympiad number theory, coding contests and public-key cryptography. Here is how to test whether a root exists, the one-line formula for primes p ≡ 3 (mod 4), and the Tonelli–Shanks algorithm for the rest, worked by hand.
Does x² ≡ a (mod p) always have a solution?
No. A number that is a square modulo an odd prime p is called a quadratic residue, and half of the numbers from 1 to p − 1 are not residues. Euler's criterion decides it: a number a not divisible by p has a square root mod p if and only if a^((p−1)/2) ≡ 1 (mod p), and a non-residue gives −1 instead (Tonelli–Shanks algorithm, Wikipedia).
For a = 10 and p = 13 you need 10⁶ mod 13. Square step by step: 10² = 100 ≡ 9, 10⁴ ≡ 81 ≡ 3, so 10⁶ ≡ 9 × 3 = 27 ≡ 1. A root exists. If residues are new to you, start with quadratic residues and the Legendre symbol; the law of quadratic reciprocity gives a faster test for large numbers.
What is the shortcut when p ≡ 3 (mod 4)?
For a prime p ≡ 3 (mod 4) and a residue a, Lagrange found that the square roots are x ≡ ±a^((p+1)/4) (mod p) (Quadratic residue, Wikipedia).
The proof is one line: x² = a^((p+1)/2) = a · a^((p−1)/2) ≡ a · 1 = a, by Euler's criterion. The exponent (p+1)/4 is a whole number exactly when p ≡ 3 (mod 4).
Example: x² ≡ 3 (mod 11). Here (11 + 1)/4 = 3, so x ≡ 3³ = 27 ≡ 5. Check: 5² = 25 = 22 + 3. The roots are 5 and 6.
For p = 13 the trick fails, because 14/4 is not a whole number. Legendre found a similar formula for p ≡ 5 (mod 8), which covers 13, but the same page notes that for primes p ≡ 1 (mod 8) there is no known formula, so you need an algorithm that works for every odd prime.
How does the Tonelli–Shanks algorithm work?
Alberto Tonelli published the method in 1891, and Daniel Shanks developed it independently in 1973 (Tonelli–Shanks algorithm, Wikipedia). Write p − 1 = Q · 2^S with Q odd, and pick a non-residue z. Start with M = S, c = z^Q, t = a^Q and R = a^((Q+1)/2). Throughout, R² ≡ a·t (mod p), so when t reaches 1, R is a root. While t is not 1, find the least i with t^(2^i) ≡ 1, set b = c^(2^(M−i−1)), then update M = i, c = b², t = t·b² and R = R·b.
Worked example: x² ≡ 10 (mod 13)
p − 1 = 12 = 3 · 2², so Q = 3 and S = 2.
Try z = 2: 2⁶ = 64 ≡ 12 ≡ −1, so 2 is a non-residue.
M = 2, c = 2³ = 8, t = 10³ = 1000 ≡ 12, R = 10² = 100 ≡ 9. Invariant check: R² = 81 ≡ 3 and a·t = 120 ≡ 3.
t is not 1. t² = 144 ≡ 1, so i = 1.
b = 8^(2^0) = 8. Now M = 1, c = 64 ≡ 12, t = 12 · 12 = 144 ≡ 1, R = 9 · 8 = 72 ≡ 7.
t = 1, so x = 7. Check: 7² = 49 = 39 + 10. The other root is 13 − 7 = 6, and 6² = 36 = 26 + 10.
Finding z is the only guess in the method, and half of all candidates work (Tonelli–Shanks algorithm, Wikipedia). Every primitive root modulo a prime is a non-residue, so one of those also serves.
Why are square roots modulo a composite as hard as factoring?
Modulo 15 = 3 · 5, the number 4 has four square roots: 2, 7, 8 and 13. The roots 2 and 7 are not negatives of each other, and gcd(7 − 2, 15) = 5 hands you a factor of 15.
Finding square roots modulo a composite number is computationally equivalent to factoring it, which is why Tonelli–Shanks cannot be used there (Tonelli–Shanks algorithm, Wikipedia).
In a January 1979 MIT technical report, Michael O. Rabin proved that anyone who can invert his public-key function for n = p·q, even for a small percentage of values, can factor n (Rabin, MIT/LCS/TR-212). The same abstract notes that breaking RSA is not known to be equivalent to factoring. This hardness is the basis of the Rabin cryptosystem.
A checklist for your own problems
Reduce a modulo p and test Euler's criterion.
If p ≡ 3 (mod 4), compute a^((p+1)/4) and stop.
Otherwise factor p − 1 = Q · 2^S, find z and run the loop, checking R² ≡ a·t each time.
Give both roots, x and p − x.
For a composite modulus with known factors, solve modulo each prime and combine.
Try x² ≡ 5 (mod 29) yourself, with Q = 7 and S = 2; the roots are 11 and 18. To compare your working, the Fermi Problem Library has a step-by-step solver that explains the reasoning rather than printing an answer. If residues still feel shaky, the Fermi Concept Library covers every concept in mathematics.



