HomeArticlesMathematics

The Ulam Spiral: Primes Hiding in Diagonals

A doodle at a boring conference turned into one of the most recognisable images in number theory — and a real, quantifiable pattern.

mysimulator teamUpdated June 2026≈ 7 min read▶ Open the simulation

A doodle that became a discovery

In 1963, mathematician Stanislaw Ulam was reportedly sitting through a long, uninteresting presentation and began doodling: he wrote the integers 1, 2, 3… in a square spiral starting at the centre, and circled every prime as he went. What should have been a scatter of dots turned out to fall along diagonal lines far more often than chance would suggest. He and colleagues Myron Stein and Mark Wells plotted a much larger version by computer soon after, and the pattern held: even out to hundreds of thousands of integers, primes visibly cluster along certain diagonals and avoid others.

live demo · spiral structure evoking the prime diagonals● LIVE

Building the spiral

The construction is entirely mechanical: place 1 at the centre of a grid, then walk outward in a square spiral — right, up, left, left, down, down, right, right, right… — placing each successive integer on the next cell. Every straight segment of the spiral of a fixed direction corresponds to the values of a specific quadratic polynomial in the step count, because the spiral's arm lengths grow by exactly one cell every half-turn. That is the algebraic seed of the whole pattern: the numbers running along any diagonal of the spiral are literally the outputs of some polynomial 4n² + bn + c for fixed integers b and c, evaluated at consecutive n.

Why some diagonals are so much richer in primes

Not all quadratics produce primes equally often. The classic example, discovered by Euler in 1772, is:

f(n) = n² + n + 41

f(0)=41  f(1)=43  f(2)=47  f(3)=53 … all prime for every
integer n from 0 to 39 — 40 consecutive prime values.

It eventually fails — f(40) = 41², obviously composite — but the run of 40 primes in a row is far longer than a "random" quadratic would give. The reason is arithmetic, not magic: for a given small prime p, count how many residues n mod p make f(n) divisible by p. A quadratic like n² + n + 41 has an unusually low count of such "bad" residues across many small primes p, meaning fewer of its outputs get automatically ruled out as composite by small-prime divisibility, so a larger fraction of its values survive as primes over any given stretch.

Hardy-Littlewood conjecture F

This intuition was made precise by G. H. Hardy and John Littlewood's 1923 conjecture F, which predicts the asymptotic density of primes among the values of a quadratic an² + bn + c in terms of a product over primes p measuring exactly this residue-avoidance effect. Diagonals of the Ulam spiral corresponding to polynomials with a high Hardy-Littlewood constant are predicted — and observed — to be visibly denser in primes than diagonals with a low constant, which is precisely the striped pattern the spiral makes visible to the eye. The conjecture remains formally unproven, but it matches empirical prime counts extremely closely.

The Sacks spiral: a smoother cousin

Robert Sacks's 1994 variant replaces the square spiral's fixed-length steps with a true Archimedean spiral, placing integer n at angle 2π√n and radius proportional to √n, so perfect squares always land on the same ray. This trades the Ulam spiral's straight diagonal lines for curved arcs, but the same underlying mechanism — quadratics tracing curves, some curves prime-rich, others prime-poor — produces an equally striking, if more organic-looking, pattern.

Frequently asked questions

Did Ulam discover the diagonal lines on purpose?

No — the story is that Ulam doodled the spiral during a boring 1963 conference talk, marking primes as he went, and was startled to see them line up along diagonals rather than scatter randomly. He and collaborators plotted it by computer soon after and confirmed the pattern held for much larger ranges.

Why does n squared + n + 41 produce so many primes?

It is one of several quadratics discovered by Euler that happen to have unusually few small prime factors dividing their values, so a larger-than-average share of outputs survive as primes for a long stretch of n. It produces primes for every integer n from 0 to 39, but it does eventually fail — at n = 40 the value is 41 squared — and it is not unique or magic, just an extreme example of the same phenomenon that makes every diagonal on the spiral more or less prime-dense.

Does the Ulam spiral prove anything about the distribution of primes?

No — it is a visualisation, not a proof. It illustrates a real and quantifiable effect predicted by the Hardy-Littlewood conjecture F, which estimates how often a given quadratic polynomial produces primes based on how many residues it excludes modulo small primes. That conjecture itself remains unproven, so the spiral is best read as striking supporting evidence rather than a demonstrated theorem.

Try it live

Everything above runs in your browser — open Ulam Spiral and change the parameters while it is running. Nothing is installed, nothing is uploaded, the whole model lives in one tab.

▶ Open Ulam Spiral simulation

What did you find?

Add reproduction steps (optional)