Presets
Playback
Shell, layer and view
Pick the shell n, the layer z to inspect, and the viewing angles. Dragging the dot cloud changes the same two angles.
Layers
Model checks
The science
The question
Which whole numbers n can be written as x2 + y2 + z2 with integers x, y, z, and in how many ways? Geometrically, r3(n) is the number of points of the cubic lattice Z3 that lie exactly on the sphere of radius √n. Signs and order count: (1,2,3) and (−3,2,1) are different points, so one orbit of the 48 symmetries of the cube has up to 48 points.
1. Legendre's three-square theorem (1798, proved by Gauss in 1801)
A natural number n is a sum of three squares unless it has the form 4a(8b + 7). So 7, 15, 23, 28, 31, 39, ... are the gaps: the sphere of that radius passes between all lattice points. The gaps have density (1/8)(1 + 1/4 + 1/16 + ...) = 1/6, so 5/6 of all n are representable. Modulo 8 a square is 0, 1 or 4, and a short case check shows that three squares never add to 7 mod 8; dividing out factors of 4 handles the rest.
2. Counting with class numbers (Gauss, Hurwitz)
The number of points is tied to binary quadratic forms. With the Hurwitz class number H(N), the weighted count of reduced positive forms a x2 + b xy + c y2 of discriminant −N (forms equivalent to a(x2+y2) count 1/2, to a(x2+xy+y2) count 1/3), Gauss's theorem reads r3(n) = 12 (H(4n) − 2 H(n)). The page counts reduced forms (|b| ≤ a ≤ c) directly and compares with brute-force enumeration of the lattice.
3. Volume law and the sphere problem
Summing over shells gives N(R) = #{|p| ≤ R} ≈ (4/3) π R3, so the average of r3(n) is 2π√n (the derivative of the volume), although the individual values scatter widely. The error N(R) − (4/3) π R3 is the Gauss sphere problem; it is conjectured to be about R1+ε and is known to be O(R21/16). Also r3(n) = ∑z r2(n − z2): cut the sphere into layers z = const and count the points on each circle.
Model checks on this page
(1) r3(n) from the class-number formula against brute-force counting for every n up to 600. (2) Legendre's theorem for every n up to 3000, and the representable share against 5/6. (3) The ball count up to n = 300 against (4/3) π R3, and the layer identity for the selected shell.
What to try
Jump to n = 7 and then press Next gap: the shell is empty. Compare n = 14 (one orbit of 48 points, all coordinates different) with n = 25 (the 6 axis points plus 24 points of type (0,3,4)). Press Run n upward and watch the ball fill; hide the inside points to see only the shell; switch to the first octant to see one representative of each orbit. On graph 3 the gold dots must sit on the grey curve: that is Gauss's formula at work.
Honest simplifications
Only shells up to n = 300 are drawn (21,879 points); the number-theory tables run further (to 3000). The weights 1/2 and 1/3 in the class number sum are the standard Hurwitz convention. The random-looking scatter of r3(n) is real arithmetic, not noise. The lattice spacing is exaggerated in the picture and larger shells are not shown.
What to try
Open n = 7 and press Next gap to see empty shells at 15, 23, 28, 31 and so on. Compare n = 14 (a single orbit of 48 points) with n = 25 (6 axis points plus 24 of type (0,3,4)). Use the slice slider to step through the layers z = const: each layer is a Gauss circle problem, and the layer counts add up to r3(n). In the graphs, the gold dots from the class-number formula must sit exactly on the brute-force curve.