Sums of squares

Sums of squares

Which numbers are sums of two squares, of three, of four? Fermat, Legendre and Lagrange answered each, and Jacobi counted the ways. Every representation of n as a sum of two squares comes out of the factorisation of n in the Gaussian integers.

Two squares

Fermat: an odd prime is a sum of two squares exactly when p≡1(mod4)p \equiv 1 \pmod 4. To find them, take xx with x2≡−1(modp)x^2 \equiv -1 \pmod p and run Euclid's algorithm on pp and xx; the first remainder below p\sqrt p is aa, and p−a2p - a^2 is a square (Cornacchia, Brillhart).
A number nn is a sum of two squares exactly when every prime ≡3(mod4)\equiv 3 \pmod 4 divides it to an even power.

import { primeAsTwoSquares, twoSquaresAll, twoSquaresReason, r2, threeSquares, fourSquares, r4, pythagoreanTriples } from "@ratmath/numtheory";

primeAsTwoSquares(13);            // [3n, 2n]
primeAsTwoSquares(1000000009n);   // [31400n, 3747n]
twoSquaresAll(325);               // [[18n, 1n], [17n, 6n], [15n, 10n]]
twoSquaresReason(21);             // { possible: false, reason: "3 ≡ 3 (mod 4) divides 21 to the odd power 1" }
r2(325);                          // 24n: with signs and order

Why: in the Gaussian integers Z[i]\mathbb Z[i], a2+b2=(a+bi)(a−bi)a^2 + b^2 = (a + bi)(a - bi). A prime p≡1(mod4)p \equiv 1 \pmod 4 splits as ππˉ\pi\bar\pi, a prime ≡3\equiv 3 stays prime, and 2=−i(1+i)22 = -i(1 + i)^2. So the elements of norm nn are found by choosing, for each pe∥np^e \parallel n with p≡1p \equiv 1, how many of the ee factors are π\pi and how many πˉ\bar\pi. Counting the choices gives Jacobi's formula

r2(n)=4(d1(n)−d3(n))=4∏p≡1(4)(ep+1) r_2(n) = 4\bigl(d_1(n) - d_3(n)\bigr) = 4\prod_{p \equiv 1 (4)} (e_p + 1)

(when no prime ≡3\equiv 3 appears to an odd power), with did_i the number of divisors ≡i(mod4)\equiv i \pmod 4. 325=52⋅13325 = 5^2 \cdot 13 gives 4⋅3⋅2=244 \cdot 3 \cdot 2 = 24.

Three squares and four

Legendre and Gauss: nn is a sum of three squares unless n=4a(8b+7)n = 4^a(8b + 7). Lagrange: every nn is a sum of four. Jacobi again counts:

r4(n)=8∑d∣n, 4∤dd. r_4(n) = 8 \sum_{d \mid n,\ 4 \nmid d} d.
threeSquares(30);    // [5n, 2n, 1n]
threeSquares(28);    // null: 28 = 4·7
fourSquares(112);    // [8n, 4n, 4n, 4n]: 112 = 16·7 needs four
r4(12);              // 96n
rk(10, 8);           // 14112n: r₈(10), by multiplying out the theta series

rk counts directly as the coefficient of qnq^n in θ(q)k\theta(q)^k, θ(q)=∑m∈Zqm2\theta(q) = \sum_{m \in \mathbb Z} q^{m^2}, which is also how the tests check the formulas.

Pythagorean triples

Every primitive triple a2+b2=c2a^2 + b^2 = c^2 with aa odd is a=m2−n2a = m^2 - n^2, b=2mnb = 2mn, c=m2+n2c = m^2 + n^2 for coprime m>n>0m > n > 0 of opposite parity (Euclid). Read in Z[i]\mathbb Z[i]: a+bi=(m+ni)2a + bi = (m + ni)^2. So the triples with hypotenuse cc are the representations of c2c^2 as a sum of two squares, and those with leg aa come from factoring a2=(c−b)(c+b)a^2 = (c - b)(c + b).

pythagoreanTriples(30);         // [[3n, 4n, 5n], [5n, 12n, 13n], [15n, 8n, 17n], [7n, 24n, 25n], [21n, 20n, 29n]]
triplesWithHypotenuse(65);      // [[16n, 63n, 65n], [25n, 60n, 65n], [33n, 56n, 65n], [39n, 52n, 65n]]
triplesWithLeg(12);             // [[12n, 5n, 13n], [12n, 9n, 15n], [12n, 16n, 20n], [12n, 35n, 37n]]
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