Quadratic reciprocity

Quadratic reciprocity

Which numbers are squares modulo p? Euler's criterion answers it with one power, and Gauss's law of quadratic reciprocity answers it faster, by turning the question upside down until it is trivial. The steps can be shown, line by line.

Residues and the Legendre symbol

For an odd prime pp and p∤ap \nmid a, aa is a quadratic residue when x2≡a(modp)x^2 \equiv a \pmod p has a solution. Exactly half of the nonzero residues are. The Legendre symbol (ap)\left(\frac ap\right) is 11 for a residue, −1-1 for a non-residue and 00 when p∣ap \mid a, and Euler's criterion computes it:

(ap)≡a(p−1)/2(modp). \left(\frac ap\right) \equiv a^{(p-1)/2} \pmod p.
import { legendre, jacobi, jacobiSteps, eulerCriterion, gaussLemma, quadraticResidues, sqrtsMod } from "@ratmath/numtheory";

quadraticResidues(11);    // [0n, 1n, 3n, 4n, 5n, 9n]
legendre(2, 7);           // 1: 3² = 9 ≡ 2
eulerCriterion(3, 7);     // { power: 6n, value: -1 }: 3³ = 27 ≡ −1 (mod 7)
gaussLemma(3, 11);        // { value: 1, mu: 2, large: [6n, 9n] }

Gauss's lemma counts instead: among a,2a,…,p−12aa, 2a, \dots, \frac{p-1}{2}a reduced into (0,p)(0, p), let μ\mu be how many exceed p/2p/2. Then (ap)=(−1)μ\left(\frac ap\right) = (-1)^\mu. For a=3a = 3, p=11p = 11 the residues are 3, 6, 9, 1, 4, and two of them are large.

The law of quadratic reciprocity

For distinct odd primes p,qp, q:

(pq)(qp)=(−1)p−12⋅q−12, \left(\frac pq\right)\left(\frac qp\right) = (-1)^{\frac{p-1}{2}\cdot\frac{q-1}{2}},

so the two symbols agree unless both primes are 3 mod 43 \bmod 4. With the two supplements (−1p)=(−1)(p−1)/2\left(\frac{-1}{p}\right) = (-1)^{(p-1)/2} and (2p)=1\left(\frac 2p\right) = 1 exactly when p≡±1(mod8)p \equiv \pm 1 \pmod 8, any symbol can be computed like a gcd: reduce the top, pull out factors of 2, flip. The Jacobi symbol (an)=∏(api)\left(\frac an\right) = \prod \left(\frac a{p_i}\right) for odd n=∏pin = \prod p_i obeys the same rules, so the flips never need a factorisation. (A Jacobi symbol of 1 does not mean aa is a square mod nn; a value of −1 does mean it is not.)

jacobiSteps(1001, 9907).steps;
// (1001/9907)
// = (9907/1001)   reciprocity: one of them is 1 mod 4, so the sign stays
// = (898/1001)    reduce the top mod 1001
// = (449/1001)    take out 2: (2/1001) = 1 since 1001 ≡ 1 (mod 8)
// …
// = −(1/29)       take out 2^3: (2/29) = −1 since 29 ≡ 5 (mod 8)
// = −1

The Kronecker symbol extends the Jacobi symbol to every bottom: (a2)\left(\frac a2\right) is 0 for even aa, 1 for a≡±1(mod8)a \equiv \pm1 \pmod 8, −1 for a≡±3a \equiv \pm 3, and (a−1)\left(\frac a{-1}\right) is the sign of aa. It is the character (D⋅)\left(\frac D\cdot\right) attached to a quadratic field, which is how it appears in class number formulas.

Square roots modulo n

Modulo a prime, Tonelli–Shanks finds a root in a few powers (from algebraic's sqrtMod). Modulo a composite, find the roots modulo each prime power by Hensel lifting and combine them: so there can be many.

sqrtsMod(1, 15);    // [1n, 4n, 11n, 14n]
sqrtsMod(4, 32);    // [2n, 6n, 10n, 14n, 18n, 22n, 26n, 30n]
sqrtsMod(-1, 65);   // [8n, 18n, 47n, 57n]
Try itEdit the code and run it with @ratmath/numtheory and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs