On this page
Square whole numbers, keep only the remainder after dividing by a prime, and something tidy happens: exactly half of the nonzero remainders show up and the other half never do.
What is a quadratic residue?
An integer q is a quadratic residue modulo n if it is congruent to a perfect square modulo n, that is, if x² ≡ q (mod n) has an integer solution x (Wikipedia: Quadratic residue). A nonzero number congruent to no square is a quadratic non-residue. Counts usually leave 0 out, a convention MathWorld also follows, and so will we.
Take p = 7 and square every nonzero remainder:
1² = 1
2² = 4
3² = 9 ≡ 2
4² = 16 ≡ 2
5² = 25 ≡ 4
6² = 36 ≡ 1
The squares mod 7 are 1, 2 and 4; the numbers 3, 5 and 6 are non-residues. Notice that the list reads the same forwards and backwards: 1, 4, 2, 2, 4, 1.
Why are exactly half the nonzero remainders squares?
That mirror is the whole reason. Since (−x)² = x², the numbers x and p − x always have the same square.
No third number joins them. If x² ≡ y² (mod p), then p divides (x − y)(x + y). A prime that divides a product divides one of the factors, so y ≡ x or y ≡ −x. The same fact is why prime factorisation is unique and 1 is not prime.
For an odd prime, x and −x are different, because x ≡ −x would mean p divides 2x. So squaring sorts the p − 1 nonzero numbers into (p − 1)/2 pairs, each landing on its own square. Modulo an odd prime there are exactly (p − 1)/2 non-residues, and so (p − 1)/2 nonzero residues (Wikipedia: Quadratic residue). The first systematic treatment of residues is Section IV of Gauss's Disquisitiones Arithmeticae (1801), as the same article records.
How do you check if a number is a quadratic residue mod p?
Euler's criterion is a direct test: for an odd prime p and a not divisible by p, a^((p−1)/2) ≡ 1 (mod p) if a is a quadratic residue and ≡ −1 (mod p) if it is not (Wikipedia: Euler's criterion). The criterion dates from a 1748 paper by Leonhard Euler, according to the same page.
The Legendre symbol (a/p) is 1 if a is a nonzero quadratic residue mod p, −1 if a is a non-residue, and 0 if p divides a; Adrien-Marie Legendre introduced it in 1797 or 1798 (Wikipedia: Legendre symbol). In that notation, Euler's criterion reads (a/p) ≡ a^((p−1)/2) (mod p).
Why it works:
By Fermat's little theorem a^(p−1) ≡ 1, so b = a^((p−1)/2) has b² ≡ 1. By the pairing argument, b is 1 or −1.
If a ≡ x², then b ≡ x^(p−1) ≡ 1, so all (p − 1)/2 residues are roots of t^((p−1)/2) − 1.
A polynomial of degree d has at most d roots mod a prime, so those are all its roots. Every non-residue gives −1.
Worked example: is 2 a square mod 17? Is 3?
Here (p − 1)/2 = 8, so square three times.
For a = 2: 2⁴ = 16 ≡ −1, so 2⁸ ≡ 1, and 2 is a residue. Check: 6² = 36 = 34 + 2.
For a = 3: 3⁴ = 81 ≡ 13, so 3⁸ ≡ 169 ≡ −1 (as 170 = 10 × 17), and 3 is a non-residue.
Use this order every time:
Reduce a mod p.
Compute a^((p−1)/2) by repeated squaring, reducing after every step, as in our RSA worked example with tiny primes.
Read it: 1 means square, −1 means not, anything else means an arithmetic slip or a modulus that is not prime.
When is −1 a quadratic residue mod p?
Put a = −1 into Euler's criterion: (−1)^((p−1)/2) is 1 when (p − 1)/2 is even and −1 when it is odd. So −1 is a square mod an odd prime p exactly when p ≡ 1 (mod 4), the first supplement to quadratic reciprocity on the Legendre symbol page.
p = 5: 2² = 4 ≡ −1, a square.
p = 13: 5² = 25 ≡ −1, a square.
p = 7: the squares are 1, 2, 4, and −1 ≡ 6 is missing.
p = 11: the squares are 1, 3, 4, 5, 9, and −1 ≡ 10 is missing.
If a step felt shaky, such as why a prime dividing a product divides a factor, revisit it in the Fermi Concept Library, which has a lesson for every concept in mathematics. To check your working line by line, the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning rather than printing an answer.



