Topic · Number Theory

Quadratic Residues and Reciprocity

Pick a prime — say 11. Which of the numbers 1, 2, 3, …, 10 can be written as a perfect square when you only care about the remainder mod 11? Square each one and you'll find only five show up: {1, 3, 4, 5, 9}. The other five never appear, no matter what you square. Those five are called quadratic residues mod 11; the rest are non-residues. The strange, deep symmetry that lets you predict which is which — for any prime, without doing the squaring — is what Gauss called his golden theorem. We know it today as the law of quadratic reciprocity, and the bookkeeping tools we'll need along the way (the Legendre symbol, Euler's criterion) earn their names as answers to that question.

What you'll leave with

  • A precise definition of quadratic residue modulo an odd prime, and why exactly half the nonzero residues qualify.
  • The Legendre symbol $\left(\tfrac{a}{p}\right)$ as a compact yes/no record of "is $a$ a square mod $p$?"
  • Euler's criterion — a one-line formula that reduces the question to a modular exponentiation — with a short proof.
  • The two supplementary laws: when $-1$ and $2$ are squares mod $p$.
  • Quadratic reciprocity itself, in both Gauss's symmetric form and the "$1 \bmod 4$ vs. $3 \bmod 4$" rephrasing.
  • How to actually use it — computing a Legendre symbol two different ways and getting the same answer.

1. What is a quadratic residue?

Quadratic residue mod p

Let $p$ be an odd prime and let $a$ be an integer with $p \nmid a$. We call $a$ a quadratic residue modulo $p$ if there exists an integer $x$ such that

$$ x^2 \equiv a \pmod{p}. $$

If no such $x$ exists, $a$ is a quadratic non-residue.

The question being asked is: among the nonzero residue classes mod $p$, which ones can be reached by squaring? Square every nonzero element, see what comes out.

Try $p = 7$. Squaring $1, 2, 3, 4, 5, 6$ modulo $7$ gives

$$ 1^2, 2^2, 3^2, 4^2, 5^2, 6^2 \;\equiv\; 1, 4, 2, 2, 4, 1 \pmod{7}. $$

The image is $\{1, 2, 4\}$ — exactly three values. The non-residues are $\{3, 5, 6\}$, also three. The split is even. That isn't a coincidence:

Proposition
Half the nonzero residues are squares

For an odd prime $p$, exactly $\tfrac{p-1}{2}$ of the residues $\{1, 2, \ldots, p-1\}$ are quadratic residues, and the other $\tfrac{p-1}{2}$ are non-residues.

Why? The map $x \mapsto x^2$ on $\{1, \ldots, p-1\}$ pairs every value with its negative: $x^2 \equiv (-x)^2 \pmod{p}$, and modulo an odd prime $x \not\equiv -x$. So the squaring map is exactly two-to-one, and its image has $(p-1)/2$ elements.

Picture

Imagine folding the multiplicative group $(\mathbb{Z}/p)^\times$ in half — every element gets identified with its negative. The squares are the "fold lines," the half-sized image left over. The non-residues are exactly the elements that disappeared in the folding.

2. The Legendre symbol

We need a compact way to say "is $a$ a square mod $p$ or not?" That's what the Legendre symbol does — it's a yes/no flag written as if it were a fraction, but it isn't one.

Legendre symbol

For an odd prime $p$ and any integer $a$,

$$ \left(\frac{a}{p}\right) = \begin{cases} +1 & \text{if } a \text{ is a quadratic residue mod } p \text{ and } p \nmid a, \\ -1 & \text{if } a \text{ is a quadratic non-residue mod } p, \\ \phantom{+}0 & \text{if } p \mid a. \end{cases} $$

The notation $\left(\tfrac{a}{p}\right)$ looks like a fraction but is one symbol. The bottom must be an odd prime; the top is any integer. Read it as "the Legendre symbol of $a$ over $p$."

From the $p = 7$ calculation above:

$$ \left(\tfrac{1}{7}\right) = \left(\tfrac{2}{7}\right) = \left(\tfrac{4}{7}\right) = +1, \qquad \left(\tfrac{3}{7}\right) = \left(\tfrac{5}{7}\right) = \left(\tfrac{6}{7}\right) = -1. $$

3. Euler's criterion

Listing squares works for tiny primes. For real primes — five, six, ten digits — we need a formula. Euler gave one.

Theorem (Euler's criterion)
A formula for the Legendre symbol

For an odd prime $p$ and any integer $a$ with $p \nmid a$,

$$ \left(\frac{a}{p}\right) \equiv a^{(p-1)/2} \pmod{p}. $$

The right side is necessarily $\pm 1 \pmod p$, so this is an equation of integers in $\{-1, +1\}$, not just a congruence.

Short proof

By Fermat's little theorem, $a^{p-1} \equiv 1 \pmod p$. Therefore

$$ \bigl(a^{(p-1)/2}\bigr)^2 - 1 \equiv 0 \pmod{p}, $$

which factors as $\bigl(a^{(p-1)/2} - 1\bigr)\bigl(a^{(p-1)/2} + 1\bigr) \equiv 0 \pmod p$. Since $p$ is prime, one factor must vanish, so

$$ a^{(p-1)/2} \equiv \pm 1 \pmod{p}. $$

Now compare cases. Case 1: $a$ is a QR. Then $a \equiv x^2$ for some $x$, so

$$ a^{(p-1)/2} \equiv x^{p-1} \equiv 1 \pmod{p}, $$

matching $\left(\tfrac{a}{p}\right) = +1$.

Case 2: $a$ is a non-residue. The polynomial $X^{(p-1)/2} - 1$ has at most $(p-1)/2$ roots mod $p$ — and we just saw every QR is a root. So no non-residue can be a root, and therefore $a^{(p-1)/2} \equiv -1 \pmod p$, matching $\left(\tfrac{a}{p}\right) = -1$. $\blacksquare$

Why this matters

Euler's criterion turns a search ("does some $x$ exist?") into a calculation (raise $a$ to a known power mod $p$ and check the sign). With fast modular exponentiation, you can decide quadratic residue-hood for a 1000-digit prime in a fraction of a second. The criterion is the theoretical hinge behind every Legendre/Jacobi-symbol algorithm.

4. Multiplicativity

The Legendre symbol respects multiplication on top:

$$ \left(\frac{ab}{p}\right) = \left(\frac{a}{p}\right)\left(\frac{b}{p}\right). $$

This drops out of Euler's criterion in one line:

$$ (ab)^{(p-1)/2} = a^{(p-1)/2} \cdot b^{(p-1)/2} \pmod{p}. $$

So the QR/non-QR pattern obeys the same sign rules as multiplying $\pm 1$: residue · residue = residue; residue · non-residue = non-residue; non-residue · non-residue = residue. The last one is the surprising one — two "bad" numbers multiplied together give a "good" one.

Multiplicativity is what makes Legendre symbols computable. To find $\left(\tfrac{n}{p}\right)$, factor $n$ into primes, evaluate the Legendre symbol of each factor, and multiply the signs. Most of number-theoretic work on residues reduces to this.

5. The two supplementary laws

Before reciprocity itself, two special cases pin down what the Legendre symbol does to $-1$ and to $2$. Both are entirely about the prime $p$ — they tell us when $-1$ and $2$ are squares, with no second prime in sight.

First supplement: is $-1$ a square mod $p$?

First supplementary law
When $-1$ is a quadratic residue

For an odd prime $p$,

$$ \left(\frac{-1}{p}\right) = (-1)^{(p-1)/2} = \begin{cases} +1 & \text{if } p \equiv 1 \pmod 4, \\ -1 & \text{if } p \equiv 3 \pmod 4. \end{cases} $$

Plug $a = -1$ into Euler's criterion and you get this immediately: $(-1)^{(p-1)/2}$ is $+1$ when $(p-1)/2$ is even (i.e. $p \equiv 1 \pmod 4$) and $-1$ otherwise.

Check: $\left(\tfrac{-1}{5}\right) = +1$ (since $5 \equiv 1 \bmod 4$), and indeed $2^2 = 4 \equiv -1 \pmod 5$. Meanwhile $\left(\tfrac{-1}{7}\right) = -1$ (since $7 \equiv 3 \bmod 4$), and $-1 \equiv 6 \pmod 7$ is not among the squares $\{1, 2, 4\}$ we found earlier.

Second supplement: is $2$ a square mod $p$?

Second supplementary law
When $2$ is a quadratic residue

For an odd prime $p$,

$$ \left(\frac{2}{p}\right) = (-1)^{(p^2 - 1)/8} = \begin{cases} +1 & \text{if } p \equiv \pm 1 \pmod 8, \\ -1 & \text{if } p \equiv \pm 3 \pmod 8. \end{cases} $$

The exponent $(p^2 - 1)/8$ is always an integer when $p$ is odd (since $p^2 \equiv 1 \bmod 8$). The case split into residues mod $8$ comes from working out when that exponent is even or odd.

The two supplements together let you handle the factors $-1$ and $2$ that pop out of any prime factorization. Combined with reciprocity, they give a complete decision procedure for every Legendre symbol.

6. Quadratic reciprocity

So far we've answered "is $a$ a square mod $p$?" by working with one prime at a time. Reciprocity is something different. It relates two completely separate questions — "is $p$ a square mod $q$?" and "is $q$ a square mod $p$?" — and says they are almost always the same question. The link between $p$ and $q$ is symmetric, with one small twist.

Theorem (Gauss, 1796)
Law of Quadratic Reciprocity

For distinct odd primes $p$ and $q$,

$$ \left(\frac{p}{q}\right)\left(\frac{q}{p}\right) = (-1)^{\frac{p-1}{2}\cdot\frac{q-1}{2}}. $$

Read it slowly. The two Legendre symbols on the left know nothing about each other — one lives "mod $p$," the other "mod $q$." Reciprocity says their product is a sign that depends only on the residues of $p$ and $q$ modulo $4$.

The cleaner restatement

The exponent $\tfrac{p-1}{2}\cdot\tfrac{q-1}{2}$ is even unless both $\tfrac{p-1}{2}$ and $\tfrac{q-1}{2}$ are odd — which happens exactly when both $p$ and $q$ are $\equiv 3 \pmod 4$. So reciprocity is the same as:

If $p \equiv 1 \pmod 4$ or $q \equiv 1 \pmod 4$, then $\left(\tfrac{p}{q}\right) = \left(\tfrac{q}{p}\right)$ — the two symbols agree. If $p \equiv 3 \pmod 4$ and $q \equiv 3 \pmod 4$, they disagree: $\left(\tfrac{p}{q}\right) = -\left(\tfrac{q}{p}\right)$.

That's the heart of it. The question of whether $p$ is a square mod $q$ is essentially the same as whether $q$ is a square mod $p$, with a single sign flip when both primes have the unlucky residue $3 \bmod 4$.

Why this is astonishing

There is no a priori reason the arithmetic of squares mod $p$ should know anything about the arithmetic of squares mod $q$. They are two different worlds. Reciprocity says the worlds are mirrors — and the mirror is rigged by a sign rule that involves nothing harder than "$p$ mod $4$."

On the proof

We don't prove quadratic reciprocity here. It deserves its reputation for difficulty: Gauss called it the theorema aureum ("golden theorem") and gave eight different proofs over his lifetime, and the modern count is well past 300. Each proof comes from a different corner of mathematics — Gauss sums, lattice points, Eisenstein's lemma, cyclotomic fields, the Stickelberger relation, even ideas from algebraic geometry.

The flavor worth remembering is the geometric one. Eisenstein's lattice-point proof realizes the exponent $\tfrac{p-1}{2}\cdot\tfrac{q-1}{2}$ as the number of integer points inside a certain rectangle, then chops the rectangle along its diagonal and shows each Legendre symbol counts the parity of lattice points on one side. The triangles match — except for an asymmetry pinned exactly by the residue of each prime mod $4$. That asymmetry is reciprocity.

Going further

Reciprocity has descendants. Higher reciprocity laws (cubic, biquadratic) generalize to roots beyond squares; the Artin reciprocity law of class field theory absorbs them all into a single statement about Galois groups. Quadratic reciprocity is the first signpost on that road.

7. Computing $\left(\tfrac{7}{11}\right)$ two ways

To see reciprocity in action, evaluate one symbol two ways: by brute force, then by the theorem. They should agree.

Way 1: direct enumeration

We want to know whether $7$ is a square mod $11$. Square the residues $1$ through $5$ (squaring $6, \ldots, 10$ would just retrace them, by the $x^2 = (-x)^2$ pairing):

$x$$x^2 \pmod{11}$
$1$$1$
$2$$4$
$3$$9$
$4$$5$
$5$$3$

The quadratic residues mod $11$ are $\{1, 3, 4, 5, 9\}$. The value $7$ is not in that list, so

$$ \left(\frac{7}{11}\right) = -1. $$

Way 2: via reciprocity

Both $7$ and $11$ are $\equiv 3 \pmod 4$. So reciprocity says the two symbols disagree:

$$ \left(\frac{7}{11}\right) = -\left(\frac{11}{7}\right). $$

Now $11 \equiv 4 \pmod 7$, and the Legendre symbol depends only on $a \bmod p$, so

$$ \left(\frac{11}{7}\right) = \left(\frac{4}{7}\right). $$

And $4 = 2^2$ is obviously a square mod anything — so $\left(\tfrac{4}{7}\right) = +1$. Therefore

$$ \left(\frac{7}{11}\right) = -(+1) = -1. $$

The two calculations agree, exactly as they must.

The strategic point

Direct enumeration is $O(p)$; reciprocity flips to the smaller modulus, reduces, and recurses. The result is an algorithm with complexity $O(\log p)$ — essentially the same as Euclid's algorithm for the GCD, and for the same structural reason.

8. Application: solvability of $x^2 \equiv a \pmod p$

The whole point of the Legendre symbol is to answer a single yes/no question without ever having to find an actual square root:

Does the congruence $x^2 \equiv a \pmod p$ have a solution?

The Legendre symbol $\left(\tfrac{a}{p}\right)$ tells you. If it's $+1$, a solution exists (in fact, exactly two, namely $\pm x$ for some $x$). If it's $-1$, no solution exists. If it's $0$, then $p \mid a$ and the only solution is $x \equiv 0$.

This is decisive in practice. Suppose you're asked whether $x^2 \equiv 219 \pmod{383}$ has a solution. Brute force would scan up to $191$ values of $x$. Instead, factor $219 = 3 \cdot 73$ and use multiplicativity:

$$ \left(\frac{219}{383}\right) = \left(\frac{3}{383}\right)\left(\frac{73}{383}\right). $$

Each factor reduces in a few steps by reciprocity and the supplementary laws. No actual square roots are computed — only signs. That's the lever the theory hands you: from a search problem to a decision problem, with a tractable algorithm in the middle.

Where this shows up

Modern cryptography lives on this. The Solovay–Strassen primality test, the Goldwasser–Micali cryptosystem, and parts of integer factorization (the quadratic sieve, in particular) all hinge on being able to decide quadratic residue-hood fast — and reciprocity is what makes that fast.

9. Common pitfalls

The Legendre symbol is not a fraction

$\left(\tfrac{a}{p}\right)$ looks like $a/p$ but isn't. You cannot reduce it, cancel common factors, or treat it as a rational number. It's a single symbol whose value is $-1$, $0$, or $+1$.

The bottom must be an odd prime

$\left(\tfrac{a}{p}\right)$ requires $p$ to be an odd prime. For composite or even moduli you need the Jacobi symbol (which extends the definition) or the Kronecker symbol — and the Jacobi symbol's value being $+1$ no longer guarantees that $a$ is a QR.

Reducing the top mod $p$ is fine; reducing the bottom is meaningless

You can always replace $a$ by $a \bmod p$ in $\left(\tfrac{a}{p}\right)$ — that's how reciprocity becomes useful. But you cannot "reduce" $p$; it sets the modulus.

Forgetting the sign rule in reciprocity

Quadratic reciprocity is not "$\left(\tfrac{p}{q}\right) = \left(\tfrac{q}{p}\right)$ always." It's that, plus a sign flip when both primes are $\equiv 3 \pmod 4$. The flip is the entire content of the theorem — drop it and you'll get the wrong answer on roughly half of all prime pairs.

10. Worked examples

Try each before opening the solution. Use reciprocity, the supplements, and multiplicativity — the goal is fluency in flipping symbols, not raw computation.

Example 1 · Is $-1$ a square mod $13$?

Since $13 \equiv 1 \pmod 4$, the first supplement gives $\left(\tfrac{-1}{13}\right) = +1$. So yes — there should be some $x$ with $x^2 \equiv -1 \pmod{13}$.

Check. $5^2 = 25 \equiv 12 \equiv -1 \pmod{13}$. ✓

Example 2 · Compute $\left(\tfrac{2}{17}\right)$

$17 \equiv 1 \pmod 8$, so by the second supplement $\left(\tfrac{2}{17}\right) = +1$. So $2$ is a square mod $17$.

Check. $6^2 = 36 \equiv 2 \pmod{17}$. ✓

Example 3 · Compute $\left(\tfrac{5}{13}\right)$

Both primes are odd and distinct. Since $5 \equiv 1 \pmod 4$, reciprocity says the symbols agree:

$$ \left(\tfrac{5}{13}\right) = \left(\tfrac{13}{5}\right) = \left(\tfrac{3}{5}\right). $$

Now check whether $3$ is a square mod $5$. The QRs mod $5$ are $\{1, 4\}$, and $3$ isn't there. So $\left(\tfrac{3}{5}\right) = -1$, and therefore $\left(\tfrac{5}{13}\right) = -1$.

Check. The QRs mod $13$ are $\{1, 3, 4, 9, 10, 12\}$ — and $5$ isn't in the list. ✓

Example 4 · Does $x^2 \equiv 30 \pmod{103}$ have a solution?

Factor: $30 = 2 \cdot 3 \cdot 5$, so by multiplicativity

$$ \left(\tfrac{30}{103}\right) = \left(\tfrac{2}{103}\right)\left(\tfrac{3}{103}\right)\left(\tfrac{5}{103}\right). $$

The $2$. $103 \equiv 7 \equiv -1 \pmod 8$, so $\left(\tfrac{2}{103}\right) = +1$.

The $3$. Both $3$ and $103$ are $\equiv 3 \pmod 4$, so reciprocity gives a sign flip:

$$ \left(\tfrac{3}{103}\right) = -\left(\tfrac{103}{3}\right) = -\left(\tfrac{1}{3}\right) = -(+1) = -1. $$

The $5$. $5 \equiv 1 \pmod 4$, so the symbols agree:

$$ \left(\tfrac{5}{103}\right) = \left(\tfrac{103}{5}\right) = \left(\tfrac{3}{5}\right) = -1 $$

(the QRs mod $5$ are $\{1, 4\}$).

Combine. $\left(\tfrac{30}{103}\right) = (+1)(-1)(-1) = +1$. So yes — $30$ is a square mod $103$, and the congruence has two solutions.

Example 5 · Use Euler's criterion to compute $\left(\tfrac{3}{11}\right)$ from scratch

Euler's criterion gives $\left(\tfrac{3}{11}\right) \equiv 3^{(11-1)/2} = 3^5 \pmod{11}$.

Compute $3^5 \bmod 11$: $3^2 = 9$, $3^4 = 81 \equiv 4 \pmod{11}$, $3^5 = 3 \cdot 4 = 12 \equiv 1 \pmod{11}$.

So $\left(\tfrac{3}{11}\right) = +1$, meaning $3$ is a square mod $11$.

Check. From the table in §7, $5^2 \equiv 3 \pmod{11}$. ✓

Sources & further reading

Reciprocity is one of the most-written-about results in number theory. Below: a primary textbook, a classical reference, a course site that goes deeper than this page, and two web references for fact-checking.

Test your understanding

A quiz that builds from easy to hard. Pick an answer to get instant feedback and a worked explanation. Your progress is saved in this browser — come back anytime to continue.

Question 1 of 1
0 correct