Integer quaternions and four squares
Quaternions with whole-number coordinates have an arithmetic like that of the integers, with primes and greatest common divisors, once one extra family is let in. The reward is Lagrange's theorem: every whole number is a sum of four squares, with the squares computed.
Lipschitz and Hurwitz
The obvious integer quaternions are the Lipschitz ones, with whole numbers. They are not enough: division with a smaller remainder can fail. To divide, you round the exact quotient to the nearest integer quaternion, and the centre of a unit cell, , is at distance exactly 1 from every corner, so the remainder can be as large as the divisor.
Hurwitz's repair is to add those centres. A Hurwitz quaternion has coordinates that are all whole numbers or all halves of odd numbers. Now the farthest any point is from the nearest Hurwitz quaternion is , the remainder is always smaller than the divisor, and Euclid's algorithm works.
import { hurwitz, hurwitzUnits, hurwitzGcd } from "@ratmath/complex/cayley";
const a = hurwitz(7, 3, -2, 5);
const b = hurwitz(2, 1, 1, 0);
a.norm(); b.norm(); // 87n, 6n: norms are whole numbers (BigInt)
const { quotient, remainder } = a.divmod(b);
String(quotient); // "5/2 + 1/2i - 5/2j + 1/2k"
String(remainder); // "k"
quotient.mul(b).add(remainder).equals(a); // true: a = quotient·b + remainder, N(remainder) < N(b)
hurwitz("1/2", "1/2", "1/2", "1/2").norm(); // 1n: a unit
hurwitzUnits().length; // 24
There are 24 units (elements of norm 1): and the sixteen . They form a group, the binary tetrahedral group, whose rotations are the twelve symmetries of a tetrahedron. A Hurwitz has add sub mul neg conj norm divmod equals, isLipschitz, toIntegers() and toQuaternion().
Greatest common divisors
Because multiplication does not commute, there are divisors on the right and on the left. divmod divides on the right, and hurwitzGcd(a, b) is a right greatest common divisor: and , and every common right divisor divides . It is determined up to one of the 24 units on the left.
const c = hurwitz(1, 2, 0, 0);
String(hurwitzGcd(hurwitz(3, 1, -2, 5).mul(c), hurwitz(2, 1, 1, 0).mul(c)));
// "2 - i", which is −i·(1 + 2i): the common factor, up to a unit
Four squares
The norm of is , and norms multiply. So if two numbers are sums of four squares, so is their product, and the product quaternion writes it as one. It remains to handle primes, and the proof is an algorithm:
- For an odd prime , the congruence has a solution (the values of and the values of cannot all be different).
- Then divides the norm of without dividing the quaternion itself.
- So the right gcd of and is neither a unit nor times one: it has norm exactly .
- If that gcd has half-integer coordinates, one of the units times it does not.
import { quaternionOfNorm, fourSquares, fourSquaresCount } from "@ratmath/complex/cayley";
String(quaternionOfNorm(7)); // "1 + i + j + 2k": 1 + 1 + 1 + 4 = 7
String(quaternionOfNorm(1000003)); // "-699 - 151i - 699j"
fourSquares(2026); // [23n, 23n, 22n, 22n]
fourSquares(310); // [15n, 7n, 6n, 0n]
fourSquares(10n ** 18n + 9n); // [1000000000n, 3n, 0n, 0n]
fourSquares(n) factors , takes a quaternion of norm for each prime factor and multiplies them. It needs the factorisation, so it refuses (code too-large) a number with two prime factors above a million; a number-theory package will lift that limit.
How many ways
Jacobi's four-square theorem counts the representations, with order and signs distinguished:
fourSquaresCount(1); // 8n: (±1, 0, 0, 0) in each of four places
fourSquaresCount(7); // 64n
fourSquaresCount(2026); // 24336n
For an odd prime this is , which is also the number of Lipschitz quaternions of norm .
@ratmath/complex and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.