Stanislaw Ulam was sitting through a boring lecture in 1963 when he started doodling. He wrote the number 1 at the centre of a square grid, then spiralled outward — 2 to the right, 3 above 2, 4 to the left of 3, 5 below 4, and so on, tracing a counter-clockwise spiral through the positive integers. Then he circled the primes. He expected nothing in particular.
What he saw was unsettling. The primes did not scatter randomly. They lined up. Long diagonal streaks cut across the grid. Some diagonals were dense with primes; others were nearly empty. The pattern was not an artefact of small numbers — it persisted as he plotted larger and larger spirals. Mathematicians have been studying the Ulam spiral ever since, and while part of the phenomenon has a tidy explanation, the full pattern is still not completely understood.
The spiral, constructed
Start with a grid. Place 1 at the centre. Walk one step right and write 2. One step up and write 3. Two steps left: 4, 5. Two steps down: 6, 7. Three steps right: 8, 9, 10. Three steps up: 11, 12, 13. The spiral grows by one step in each of the four compass directions, then increases the step-length by one every half-turn. Eventually every positive integer has a place on the grid.
Now mark every prime. Leave composites blank.
What makes a diagonal
Here is the algebraic secret. Every diagonal line in the Ulam spiral is the graph of a quadratic polynomial in a spiral-step variable n. The reason is that walking along a diagonal in the spiral takes you to successive "shells" of the spiral, and the number of integers in a full shell of size k is 8k — so the shell boundaries are quadratic in k. Two consecutive positions on the same diagonal always differ by a value of the form 4n^2 + \text{(linear in } n\text{)} + \text{(constant)}.
So when a diagonal is crowded with primes, it is saying: this particular quadratic polynomial generates an unusually high proportion of primes.
The most famous example is Euler's polynomial f(n) = n^2 + n + 41, which happens to pass through a diagonal in the spiral. For n = 0, 1, 2, \ldots, 39, f(n) is prime every single time. That is 40 primes in a row from a simple quadratic. At n = 40, the polynomial returns 40^2 + 40 + 41 = 1681 = 41^2, finally breaking the streak. Why it must eventually fail: f(41) = 41^2 + 41 + 41 = 41(41 + 1 + 1) = 41 \cdot 43. Any polynomial f(n) with integer coefficients satisfies f(n + f(0) \cdot k) \equiv f(n) \pmod{f(0)}, which forces infinitely many composite outputs — no non-constant integer polynomial can be prime for every n.
What diagonals are empty
The anti-pattern is just as striking. Certain diagonals contain almost no primes. Why?
Because the quadratic governing the diagonal has a fixed small factor. For example, any diagonal running through the squares 1, 4, 9, 16, 25, \ldots can never be prime for n > 2 — n^2 is composite for all n \geq 2. Similarly, a diagonal whose quadratic factors as (n + a)(n + b) over the integers produces 0 primes (beyond the isolated case n = 0 or n = 1). The empty streaks are proof-by-picture that those particular quadratic forms are "prime-hostile."
What mathematicians still do not know
The Ulam spiral's full richness goes beyond what any single polynomial can explain. The deep question — which quadratic polynomials in n produce infinitely many primes? — remains open. This is a special case of Schinzel's hypothesis H and Bunyakovsky's conjecture, both of which generalise Dirichlet's theorem on primes in arithmetic progressions (which says primes are evenly distributed among residue classes modulo any fixed integer). Bunyakovsky conjectured in 1857 that any polynomial f(n) with no "obvious obstruction" (no fixed divisor and no factorisation) produces infinitely many primes. Nobody has proved this for a single polynomial of degree \geq 2. Not one.
So when you look at a dense diagonal in the Ulam spiral and wonder why so many primes sit on it, you are looking at a special case of one of the deepest open questions in mathematics — one that Dirichlet nearly answered for lines (linear polynomials), but which remains mysterious for parabolas.
A note on "randomness"
A common misreading of the Ulam spiral: "the primes are not random after all." But primes were never supposed to be uniformly random. They are pseudo-random — unpredictable in detail but obeying average density laws (the Prime Number Theorem: \pi(N) \sim N / \ln N). The spiral's diagonals do not contradict this density; they are biased relative to average density, and the bias comes from the algebraic structure of the quadratic polynomials along those diagonals.
In other words: primes are not evenly sprinkled on the plane, but they are also not patterned in any simple way. They are patterned in a deep way — and the Ulam spiral shows the patterns without explaining them.
Try it yourself
With graph paper and a pen, you can lay down a 21 \times 21 spiral (integers 1 through 441) and circle the primes in about ten minutes. The diagonals are visible by eye. Extend to 51 \times 51 (up to 2601) and the streaks become undeniable. Euler's polynomial, n^2 + n + 41, traces a single diagonal and contributes about 40 primes to the visible streak — a disproportionate share given there are only about 60 total primes near that diagonal.
This is the gentlest way into experimental number theory: draw a picture, mark the primes, and ask why.
Related: Number Theory Basics · Sieve of Eratosthenes — Composites Vanish in Waves · Why One is Not a Prime Number · The Euclidean Algorithm as Rectangle-Tiling · Number Systems