Number Theory

Quadratic Residues and the Legendre Symbol

Learn which remainders are squares modulo a prime, how Euler's criterion and the Legendre symbol decide it, and how to compute quickly.

Key takeaways

  • For an odd prime p, exactly half of the nonzero remainders are squares and half are not.
  • The Legendre symbol (a/p) is 1 for a residue, −1 for a nonresidue and 0 if p divides a.
  • Euler's criterion says (a/p) is congruent to a raised to the power (p − 1)/2 modulo p.
  • The symbol is multiplicative, so it can be computed from the symbols of the factors of a.
  • Quadratic reciprocity links (p/q) with (q/p) and makes hand computation fast.

A quadratic residue modulo a prime p is a nonzero remainder that is the square of something. Modulo 11, the number 5 is a residue because 4² = 16 leaves remainder 5, while 2 is not, since no square ever leaves remainder 2. The Legendre symbol is a compact notation that records the answer as 1 or −1.

The surprise is that you can decide whether a number is a square without searching for the square root. A single power calculation, or a few elegant rules, will do it.

Finding the squares

Square every nonzero remainder modulo 11.

x           1  2  3  4  5  6  7  8  9  10
x² mod 11   1  4  9  5  3  3  5  9  4   1

The squares are 1, 3, 4, 5 and 9. The nonresidues are 2, 6, 7, 8 and 10. The second half of the table mirrors the first, because x and p − x have the same square.

That mirroring explains the count. Each residue has exactly two square roots, since x² ≡ y² means p divides (x − y)(x + y). So p − 1 numbers produce (p − 1)/2 different squares. For the notation, see modular arithmetic.

The Legendre symbol

For an odd prime p and an integer a, Adrien-Marie Legendre's symbol is written (a/p). It looks like a fraction but is not one.

value meaning
(a/p) = 1 p does not divide a, and x² ≡ a has a solution
(a/p) = −1 x² ≡ a has no solution
(a/p) = 0 p divides a

So (5/11) = 1 and (2/11) = −1. The symbol depends only on the remainder of a modulo p, so (16/11) = (5/11).

Euler's criterion

For an odd prime p, a^((p−1)/2) ≡ (a/p) (mod p).

Test it modulo 11, where (p − 1)/2 = 5.

a = 3:   3⁵ = 243 = 22 × 11 + 1      → 1, residue
a = 2:   2⁵ = 32  = 3 × 11 − 1       → −1, nonresidue

Why is it true? Let b = a^((p−1)/2) with p not dividing a. By Fermat's little theorem, b² ≡ 1, so b is 1 or −1. If a ≡ x², then b ≡ x^(p−1) ≡ 1. So all (p − 1)/2 residues make b equal 1. A polynomial of degree (p − 1)/2 has at most that many roots modulo a prime, so no nonresidue can also give 1. Each must give −1.

With repeated squaring, this test is fast even for primes with hundreds of digits.

Multiplicativity

Since powers of products split, Euler's criterion gives a very useful rule.

(ab/p) = (a/p) × (b/p)

In words: a residue times a residue is a residue, a residue times a nonresidue is a nonresidue, and two nonresidues multiply to a residue. Modulo 11, the nonresidues 2 and 6 give 12 ≡ 1, a square.

Multiplying residues and nonresidues modulo an odd prime
Square × square Gives a square: (+1)(+1) = +1 Square × non-square Gives a non-square: (+1)(−1) = −1 Non-square × square Gives a non-square: (−1)(+1) = −1 Non-square × non-square Gives a square: (−1)(−1) = +1 Square × square Gives a square: (+1)(+1) = +1 Square × non-square Gives a non-square: (+1)(−1) = −1 Non-square × square Gives a non-square: (−1)(+1) = −1 Non-square × non-square Gives a square: (−1)(−1) = +1

Residues through primitive roots

There is a second way to see the half-and-half split. Every prime modulus has a primitive root, a number g whose powers run through all the nonzero remainders. Modulo 11 we can take g = 2.

exponent k    1  2  3  4  5   6  7  8  9  10
2^k mod 11    2  4  8  5  10  9  7  3  6  1

The even exponents give 4, 5, 9, 3 and 1, exactly the list of squares. The odd exponents give 2, 8, 10, 7 and 6, exactly the nonresidues. That is no coincidence: an even power g^(2j) is the square of g^j.

So being a residue is a matter of the parity of the exponent, and multiplying numbers adds exponents. The sign rule for products is then the familiar fact that even plus even is even, odd plus odd is even and even plus odd is odd.

The picture also shows why finding a square root can be harder than detecting one. Halving the exponent would solve the problem, but finding the exponent of a given remainder is the discrete logarithm problem, which has no known fast classical method for large primes. Euler's criterion reads only the parity, and it does so quickly.

Two special values

When is −1 a square? Euler's criterion gives (−1)^((p−1)/2).

(−1/p) = 1   if p ≡ 1 (mod 4)      e.g. 5² ≡ −1 (mod 13)
(−1/p) = −1  if p ≡ 3 (mod 4)      e.g. modulo 7 or 11

The value of (2/p) depends on p modulo 8.

(2/p) = 1    if p ≡ 1 or 7 (mod 8)     e.g. 3² ≡ 2 (mod 7)
(2/p) = −1   if p ≡ 3 or 5 (mod 8)     e.g. p = 11

These two facts are called the supplements to the reciprocity law.

Quadratic reciprocity

For two different odd primes p and q, the symbols (p/q) and (q/p) are equal, unless both primes leave remainder 3 on division by 4, in which case they are opposite.

This is astonishing: whether p is a square modulo q has no obvious connection to whether q is a square modulo p. Gauss gave the first complete proof, and the full story is in quadratic reciprocity.

A worked computation

Is 30 a square modulo 53? Factor, then reduce.

(30/53) = (2/53) × (3/53) × (5/53)

(2/53):  53 ≡ 5 (mod 8)                 → −1
(3/53):  53 ≡ 1 (mod 4), so flip:
         (53/3) = (2/3)                 → −1
(5/53):  5 ≡ 1 (mod 4), so flip:
         (53/5) = (3/5)                 → −1

product: (−1)(−1)(−1) = −1

So 30 is not a square modulo 53. Checking all the squares from 1² to 26² would reach the same conclusion with far more work.

How residues are distributed

If you list the residues modulo a large prime, they look scattered, much like coin flips. For p ≡ 1 (mod 4) the list is symmetric, since a and −a share the same status. For p ≡ 3 (mod 4) exactly one of a and −a is a residue.

The first nonresidue is usually very small. About half of all primes make 2 a nonresidue, half make 3 a nonresidue, and so on, with these conditions behaving independently across primes. So for a typical prime, one of the first few small numbers already fails to be a square. Proving strong bounds on the smallest nonresidue for every prime is surprisingly hard and is tied to deep conjectures about L-functions.

You can flip the question around. Fix a and ask for which primes p it is a square. Reciprocity answers that the pattern depends only on the remainder of p modulo 4a.

(3/p) = 1    if p ≡ 1 or 11 (mod 12)      e.g. p = 11: 5² = 25 ≡ 3
(3/p) = −1   if p ≡ 5 or 7 (mod 12)       e.g. p = 5 and p = 7

Such periodicity is far from obvious from the definition, and explaining it in more general settings leads into algebraic number theory.

Finding the square root

The symbol says whether a root exists, not what it is. For primes with p ≡ 3 (mod 4) there is a neat formula: if a is a residue, then a^((p+1)/4) is a square root.

p = 11, a = 5
5^((11+1)/4) = 5³ = 125 ≡ 4
check: 4² = 16 ≡ 5

The other root is the negative, 7. For other primes, the Tonelli-Shanks algorithm finds a root efficiently, using any known nonresidue, which random trials supply quickly.

Composite moduli and the Jacobi symbol

Jacobi extended the notation to any positive odd modulus n by multiplying the Legendre symbols for its prime factors, with repetition. It obeys the same reciprocity rules and can be computed without factoring.

But its meaning changes. A value of −1 still proves that a is not a square. A value of 1 proves nothing.

(2/15) = (2/3) × (2/5) = (−1)(−1) = 1
yet no square leaves remainder 2 modulo 15

Modulo a product of two large primes, deciding whether a number with Jacobi symbol 1 is a square is believed to be hard without the factors. Some cryptographic schemes rely on that belief. Combining roots across prime factors uses the Chinese remainder theorem.

Where residues appear

Which equations have solutions. Any quadratic congruence modulo an odd prime can be turned into a question about one square root by completing the square. The discriminant b² − 4ac decides it.

x² + x + 3 ≡ 0 (mod 11)
discriminant = 1 − 12 = −11 ≡ 0       one repeated root, x ≡ 5
check: 25 + 5 + 3 = 33 ≡ 0

Primes in quadratic formulas. Whether a prime ever divides n² + n + 41 depends on whether −163 is a square modulo that prime, which explains patterns in the Ulam spiral.

Sums of squares. The value of (−1/p) is a first step in deciding which primes are sums of two squares.

Primality testing. The Solovay-Strassen test compares the Jacobi symbol with Euler's power. A mismatch proves that a number is composite.

A procedure to follow by hand

To decide whether a is a square modulo an odd prime p, work in this order.

  1. Reduce a modulo p. If the result is 0, the symbol is 0.
  2. Pull out any perfect square factors, which always contribute 1.
  3. Factor what remains into primes and split the symbol into one piece for each.
  4. Use the rule for (2/p) on any factor of 2.
  5. Flip each odd prime factor with reciprocity, watching for the case where both are 3 modulo 4.
  6. Repeat until every symbol is small enough to read from a list of squares.

Try it on (18/23). Since 18 = 9 × 2 and 9 is a square, the answer is (2/23). Because 23 leaves remainder 7 on division by 8, the symbol is 1. Indeed 8² = 64, which leaves remainder 18 on division by 23.

Common mistakes

Reading (a/p) as division. It is a yes-or-no marker, and you cannot cancel it like a fraction.

Using reciprocity on non-primes with the Legendre symbol. Factor the top first, or switch to the Jacobi rules with their conditions.

Forgetting the prime 2. The symbol is defined for odd primes; modulo 2, every remainder is a square.

Forgetting the second root. If x works, so does −x.

Written for 4Mathematics with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 59 articles in Number Theory →

Latest news on this topic