The 240 shortest vectors of the E8 lattice projected onto a plane, forming eight concentric rings of thirty white points on a dark background, joined by faint lines between neighbouring vectors
Unit 11 · Advanced and Advanced + Analytic Number Theory · 70 min

Sums of Squares and Quadratic Forms

Fermat's two squares, Lagrange's four squares, and the binary quadratic forms behind them
Image: Own rendering

Which integers are sums of two squares, of three, of four? This unit proves Fermat's theorem that every prime of the form 4k + 1 is a sum of two squares, finds the two squares with Euclid's algorithm, counts the representations with Jacobi's formula, and proves Lagrange's four-square theorem by descent. It then introduces the binary quadratic forms behind these results, reduces them, counts their classes, and shows how the two classes of discriminant minus 20 explain the two factorizations of 6, before the lattice picture leads to Gauss's circle problem and to the sphere packing in eight dimensions.

Preview: the beginning of the unit

The number 97 is a sum of two squares, \(81 + 16\), and the number 21 is not, although both are odd and both leave the remainder 1 on division by 4; the difference is that \(21 = 3 \cdot 7\) has the prime factors 3 and 7, which both leave the remainder 3. Which integers are sums of two squares, of three, of four? The questions were asked by Diophantus of Alexandria (third century AD), answered in part by Fermat, and settled by Euler, Lagrange, Legendre, and Gauss between 1749 and 1801; the answers depend on a new object, the quadratic form \(ax^2 + bxy + cy^2\), whose theory fills the longest section of Gauss’s book Disquisitiones Arithmeticae (1801). This unit proves the theorems on two and four squares, states the one on three, and follows the forms far enough to see why \(6\) factors in two ways among the numbers \(a + b\sqrt{-5}\) of Unit 3, written \(\mathbb{Z}[\sqrt{-5}]\), and why a sphere packing in eight dimensions is a question of number theory.

1 Fermat’s theorem on sums of two squares

A square is congruent to \(0\) or \(1\) modulo 4, because \((2k)^2 = 4k^2\) and \((2k + 1)^2 = 4k(k + 1) + 1\), so a sum of two squares is congruent to \(0\), \(1\), or \(2\), and no integer of the form \(4k + 3\) is a sum of two squares. For primes this leaves 2, which is \(1 + 1\), and the primes \(p \equiv 1 \pmod 4\); the small ones, \(5 = 1 + 4\), \(13 = 4 + 9\), \(17 = 1 + 16\), and \(29 = 4 + 25\), all pass the test. Fermat claimed in a letter to Mersenne of 25 December 1640 that every prime \(p \equiv 1 \pmod 4\) is a sum of two squares, and in exactly one way. The proof needs two ingredients from earlier units. Unit 10 proved, as the first supplement to the law of quadratic reciprocity, that \(-1\) is a square modulo an odd prime \(p\) exactly when \(p \equiv 1 \pmod 4\), so for such a prime there is an integer \(a\) with \(a^2 \equiv -1 \pmod p\). The second ingredient is the pigeonhole principle of Unit 1, in a form that Axel Thue (1863-1922) published in 1902.

The proof is a pigeonhole count. Let \(k = \lfloor \sqrt{p} \rfloor\) be the largest integer below \(\sqrt{p}\), so that \(k < \sqrt{p} < k + 1\) (the square root of a prime is never an integer), and consider the \((k + 1)^2\) pairs \((u, v)\) with \(0 \le u, v \le k\). The \((k + 1)^2\) numbers \(u - av\) take at most \(p\) different residues modulo \(p\), and \((k + 1)^2 > p\), so two different pairs \((u_1, v_1)\) and \((u_2, v_2)\) give the same residue, and the differences \(x = u_1 - u_2\) and \(y = v_1 - v_2\) satisfy \(x \equiv ay \pmod p\), \(|x| \le k < \sqrt{p}\), and \(|y| \le k < \sqrt{p}\). They are not both zero because the pairs differ, which proves the lemma.