Folioby Interconnected
Log InSign Up

Quadratic residues: why exactly half the nonzero elements mod p are squares

The squaring map x -> x^2 is 2-to-1, which forces exactly (p-1)/2 quadratic residues. We develop the Legendre symbol, Euler's criterion, the law of quadratic reciprocity, and the Tonelli-Shanks algorithm for extracting square roots modulo a prime.

FO
Folio Official
March 1, 2026

Modulo 7, the squares of the nonzero elements are 12=1, 22=4, 32=2, 42=2, 52=4, 62=1. The set of values is {1,2,4}– exactly half of {1,2,3,4,5,6}. This is no coincidence. In this article we explain why, modulo any odd prime, precisely half of the nonzero residues are squares, and we develop the theory that grows from this observation.

1 Definition of quadratic residues

Definition 1 (Quadratic residue).
Let p be an odd prime and gcd(a,p)=1. If there exists x such that x2≡a(modp), then a is a quadratic residue modulo p. Otherwise, a is a quadratic non-residue.
Example 2.
For p=7, squaring each element of {1,2,…,6} modulo 7:

12≡1,22≡4,32≡2,42≡2,52≡4,62≡1.

The quadratic residues are {1,2,4}; the non-residues are {3,5,6}.

2 Why exactly half: the squaring map is 2-to-1

Theorem 3.
Let p be an odd prime. Among 1,2,…,p−1, exactly (p−1)/2 are quadratic residues and exactly (p−1)/2 are quadratic non-residues.
Proof.
Define f:(Z/pZ)∗→(Z/pZ)∗ by f(x)=x2. Since x2≡(−x)2(modp), each x and p−x (≡−x) map to the same value. Moreover x=p−x because p is odd (so 2x≡0(modp)).

Conversely, if x2≡y2(modp), then p∣(x−y)(x+y). By Euclid's lemma, either x≡y or x≡−y(modp).

Thus each quadratic residue is the image of exactly 2 elements. The number of distinct images is (p−1)/2. □
Remark 4.
Intuitively, pair up {1,2,…,p−1} into (p−1)/2 pairs {x,−x}. Each pair produces one quadratic residue. So there are exactly (p−1)/2 of them.

3 The Legendre symbol

Definition 5 (Legendre symbol).
For an odd prime p and gcd(a,p)=1,
(pa​)={1−1​if a is a quadratic residue mod p,if a is a quadratic non-residue mod p.​
When p∣a, we set (pa​)=0.
Remark 6.
The Legendre symbol is completely multiplicative: (pab​)=(pa​)(pb​). This encodes the rules "residue × residue = residue," "residue × non-residue = non-residue," and "non-residue × non-residue = residue" – precisely the multiplication table of {1,−1}.

4 Euler's criterion: an efficient test

Theorem 7 (Euler's criterion).
For an odd prime p with gcd(a,p)=1,
(pa​)≡a(p−1)/2(modp).
Proof.
If a is a quadratic residue, write a≡x2. Then a(p−1)/2≡xp−1≡1(modp) by Fermat's little theorem.

If a is a non-residue: the polynomial xp−1−1≡0(modp) has at most p−1 roots. Factor it as xp−1−1=(x(p−1)/2−1)(x(p−1)/2+1). The (p−1)/2 quadratic residues supply (p−1)/2 roots of x(p−1)/2−1, exhausting all its roots. The remaining (p−1)/2 elements (the non-residues) must satisfy x(p−1)/2≡−1. □
Remark 8.
Euler's criterion allows us to determine whether a is a quadratic residue in O(logp) time via repeated squaring, without factoring a.

5 The law of quadratic reciprocity: the most surprising theorem

Theorem 9 (Quadratic reciprocity (Gauss)).
For distinct odd primes p and q,
(qp​)(pq​)=(−1)2p−1​⋅2q−1​.
Remark 10 (Why this is surprising).
(qp​) asks whether p is a square modulo q. (pq​) asks whether q is a square modulo p. These are questions about two entirely different worlds– arithmetic modulo q and arithmetic modulo p– yet the law of quadratic reciprocity links them. The sign flips precisely when p≡q≡3(mod4); otherwise the two symbols agree.

Gauss called this the "golden theorem" (theorema aureum) and gave eight different proofs over his lifetime.
Example 11.
Is 23 a quadratic residue modulo 59? Rather than computing directly, apply reciprocity:
(5923​)(2359​)=(−1)11×29=(−1)319=−1.
Since 59≡13(mod23), we have (2359​)=(2313​). The law can be applied repeatedly to reduce the problem to trivial cases.

6 The Tonelli–Shanks algorithm

Once we know that a is a quadratic residue modulo p, we may want to find an actual square root.

Remark 12 (Tonelli–Shanks).
The Tonelli–Shanks algorithm computes a solution x to x2≡a(modp). Write p−1=2sq with q odd, find a quadratic non-residue n, and iteratively construct the square root. The running time is O(log2p).

In the special case p≡3(mod4), the answer is simply x≡a(p+1)/4(modp), since x2≡a(p+1)/2=a⋅a(p−1)/2≡a⋅1=a.

7 Takeaway

  • Exactly (p−1)/2 of the nonzero residues modulo p are quadratic residues (because x↦x2 is a 2-to-1 map).

  • The Legendre symbol encodes quadratic residuosity as ±1 and is completely multiplicative.

  • Euler's criterion provides an O(logp) test.

  • The law of quadratic reciprocity is a remarkable link between the arithmetic of two different primes.

  • The Tonelli–Shanks algorithm efficiently extracts square roots modulo p.

In the next article, we discover another face of the Euclidean algorithm – continued fractions and the theory of best rational approximations.

Number TheoryMathematicsBetween the Lines
FO
Folio Official

Mathematics "between the lines" — exploring the intuition textbooks leave out, written in LaTeX on Folio.

1 followers·107 articles
Number Theory — Between the LinesPart 6 of 8
Previous
The Chinese Remainder Theorem: why simultaneous congruences can be merged
Next
Continued fractions: how to find the best rational approximation to any number

Share your expertise with the world

Write articles with LaTeX support, build your audience, and earn from your knowledge.

Start Writing — It's Free

More from Folio Official

Folio Official·March 1, 2026

Why primes are the atoms of the integers: what the Fundamental Theorem of Arithmetic really says

Unique prime factorization is not as obvious as it seems. Through a counterexample in Z[sqrt(-5)], we witness uniqueness break down, then prove both the existence and uniqueness parts of the Fundamental Theorem, and survey the Sieve of Eratosthenes and the linear sieve.

Number TheoryMathematicsBetween the Lines
Folio Official·March 1, 2026

The Chinese Remainder Theorem: why simultaneous congruences can be merged

We view CRT as the number-theoretic incarnation of divide-and-conquer, prove the ring isomorphism Z/mnZ = Z/mZ x Z/nZ, and show how Garner's algorithm avoids overflow in practice.

Number TheoryMathematicsBetween the Lines
Folio Official·March 1, 2026

What does it mean to "divide evenly"? Why number theory begins with mod

The modular reduction map is a projection that deliberately discards information. Clocks, days of the week, and the competitive-programming modulus 10^9+7 all share the same algebraic structure. From the definition of divisibility through congruences to the ring Z/nZ, we uncover the reason number theory starts where it does.

Number TheoryMathematicsBetween the Lines
Folio Official·March 1, 2026

Why the Euclidean algorithm finds the greatest common divisor: from subtraction to division

The recursion gcd(a,b) = gcd(b, a mod b) hides an invariant: the set of common divisors never changes. Starting from a naive subtraction-based method and upgrading to division, we derive Bezout's identity, the extended Euclidean algorithm, and modular inverses.

Number TheoryMathematicsBetween the Lines