Case Study · Math · Number Theory
Striking Out Multiples on a Spiral
The idea
The story is that in 1963 Stanisław Ulam sat through a dull talk, doodled the integers in a square spiral, and started circling the primes. He expected noise. He got diagonals.
That is the whole hook, and it survives checking. Lay 1 at the centre, 2 to its right, 3 above that, and keep turning left; mark every prime. The marks do not scatter. They queue up along diagonal lines that are long enough and straight enough that you notice them before you have any idea why they are there.
The prime races project already races three algorithms for finding primes. This one is about what the answer looks like once you have it, and it makes the sieve itself the animation: instead of showing a finished picture, the plate strikes out multiples of 2, then of 3, then of 5, and lets you watch each pass scatter across the spiral in the strange broken pattern that a simple arithmetic progression turns into once you wrap it around a square.
Why there are diagonals
This part is not mysterious, and it is worth separating cleanly from the part that is.
Walk out along any diagonal of the spiral and the values you land on are quadratic in the number of steps. That is forced by the geometry: each full loop of the spiral is four sides whose lengths grow linearly, so the total count grows like the square. Different diagonals are different quadratics, all of the form .
So “the primes lie on diagonals” is really “the primes lie on the values of certain quadratic polynomials”, which is a statement about polynomials and not about spirals at all. And some quadratics are far richer in primes than others, for a reason that is elementary: if a polynomial happens to have no roots modulo any small prime, then none of its values are divisible by any small prime, so its values are much likelier than average to be prime.
The famous example is Euler’s polynomial,
which is prime for every from 0 to 39. At it finally fails, and it fails in the most inevitable way available: . You can see that coming without computing anything, since at every term is divisible by 41, and 40 is close enough to be suspicious.
The plate does not assert any of this. It lets you set the constant in , lights the matching cells, and reports the prime density along that diagonal against the density of the window it sits in.
started at 1 · 16 of 41 diagonal cells prime
started at 41 · 40 of 41 diagonal cells prime
| window | integers | all primes | along n² + n + 41 | ratio |
|---|---|---|---|---|
| 41 × 41 | 1 to 1,681 | 263 · 15.6% | 40 of 41 · 97.6% | 6.2× |
| 101 × 101 | 1 to 10,201 | 1,252 · 12.3% | 87 of 101 · 86.1% | 7.0× |
| 201 × 201 | 1 to 40,401 | 4,236 · 10.5% | 157 of 201 · 78.1% | 7.4× |
Both spirals above wind the same integers the same way; only the number at the centre differs. The diagonal ruled across each is one line of cells, and it holds 16 primes when the spiral starts at 1 and 40 of 41 when it starts at 41, because in that second spiral the line is exactly n² + n + 41, prime for every n from 0 to 39 and breaking first at n = 40, where the value 1,681 is 41 × 41. With JavaScript enabled this plate runs the sieve itself across the spiral, one prime at a time, and lets you move the centre, resize the grid, and change c.
The numbers
Everything below was measured by the plate’s own engine, whose sieve is checked integer by integer against trial division (70,287 integers across seven windows, zero disagreements).
| Window | Integers | Primes | Density | On | Diagonal density | Ratio |
|---|---|---|---|---|---|---|
| 41 × 41 from 1 | 1 to 1,681 | 263 | 15.65% | 40 of 41 | 97.56% | 6.24× |
| 51 × 51 from 1 | 1 to 2,601 | 378 | 14.53% | 47 of 51 | 92.16% | 6.34× |
| 101 × 101 from 1 | 1 to 10,201 | 1,252 | 12.27% | 87 of 101 | 86.14% | 7.02× |
| 201 × 201 from 1 | 1 to 40,401 | 4,236 | 10.48% | 157 of 201 | 78.11% | 7.45× |
Two things in that table are worth pausing on.
The diagonal is six to seven times denser in primes than its own neighbourhood, and it stays that way as the window grows. This is a large effect, not a borderline statistical one, and it is why the diagonals are visible to the naked eye rather than requiring a chi-squared test.
But the diagonal’s own density is falling, from 97.56% to 78.11%, while the ratio to the background is rising, from 6.24 to 7.45. Both are exactly what should happen. Primes thin out logarithmically, so every density on the page falls; the ratio rises because the quadratic’s advantage is a fixed structural head start (immunity to a set of small divisors) applied to a background that is getting sparser.
The starting number, and the cheapest dramatic result available
Here is the one-line change with the best payoff in the whole project. Start the spiral at 41 instead of at 1.
Nothing about the arithmetic changes. But the spiral’s coordinates shift, and Euler’s polynomial, which was scattered along a diagonal that ran off the edge, now lands entirely on one straight unbroken line. In the same 41-cell diagonal, a spiral started at 1 holds 16 primes; started at 41 it holds 40.
I like this because it is a lesson about pictures rather than about primes. The mathematical content is identical in both images. What changed is the alignment between a structure that was always there and the grid used to draw it. A visual pattern is a fact about the data and about the presentation, and telling those two apart is most of the work of reading any figure well.
What is actually unsolved here
It would be easy to leave the impression that the diagonals are fully explained. They are not, and the gap is worth stating precisely.
We can explain why some quadratics are prime-rich: the small-divisor argument above is elementary. What nobody can prove is that any of them stay that way forever. It is not known whether any quadratic polynomial takes infinitely many prime values. Not , not , not any of them. The conjecture that they do, with a predicted density, is due to Hardy and Littlewood in 1923, and it is still a conjecture. Bunyakovsky’s conjecture, which covers the general case, dates to 1857 and is likewise open.
So the summary is that the Ulam spiral is a picture of a real and substantial effect whose local cause is understood and whose global behaviour is one of the older open questions in number theory. That is a better ending than a tidy one.
Build notes
The build note worth recording is that the sieve had to be genuinely segmented rather than a full sieve up to the largest value, because the window can start anywhere; the base primes come from the existing Eratosthenes implementation in this site’s prime races engine, so there is exactly one sieve in the repository and this project borrows it rather than growing a second.
The rendering constraint was the interesting one. A 201 × 201 spiral is 40,401
cells, which is far too many DOM nodes, so the field is painted as a
one-pixel-per-cell ImageData and blitted with smoothing off. The whole island
is a little over four kilobytes gzipped, and I am more pleased about that than is
entirely reasonable.