← All Labs
INTERACTIVE APPLET · NUMBER THEORY & PRIMES

The Ulam Spiral Lighting Up Primes

In 1963 Stanisław Ulam, doodling through a dull lecture, wound the whole number line into a square spiral — 1, 2, 3, 4 … coiling outward — and shaded in the primes. What should have been a random dusting of dots instead snapped into diagonal streaks. Those aren't an illusion: each diagonal is the track of a quadratic polynomial \(4n^2+bn+c\), and some quadratics are astonishingly prime-rich. Spiral the integers, glow the primes, and lay a polynomial along a diagonal to see the hidden order in what looks like chaos.

n ↦ (x, y) spiral · prime(n)?

The primes are not scattered — they ride diagonals

Hover a cell to read its number · scroll or drag the sliders to zoom & move the origin
Canvas · sieve of Eratosthenes

DRIVE IT

prime composite highlighted diagonal polynomial track 4n²+bn+c the origin cell

PRIME DENSITY

PRIMES IN VIEW
0%

ORIGIN (centre value)

VIEW

PRIME-GENERATING POLYNOMIAL

P(n) = n² + n +

SHOW

POLY HIT-RATE  first 100 n

WALKTHROUGH

    AHA
    Winding the integers into a spiral makes every diagonal line obey the same quadratic rule — step out along one and the value jumps by \(4n^2+bn+c\). Primes can only ever fall on these quadratic tracks, and certain quadratics (Euler's \(n^2+n+41\) gives 40 primes in a row) are so prime-dense that their diagonal blazes with dots while its neighbours stay dark. The "random" scatter was never random: it was a collision of every quadratic residue pattern at once. Primes carry a deep hidden structure — we just needed the right coordinates to see it.
    Move the origin to 41 and the central diagonal is Euler's polynomial itself — an unbroken orange run. Slide it to a boring value and the streak dissolves. The order was in the *labelling*, not the primes.

    EXPLANATION

    Start at the centre and walk the integers outward in a square spiral: 1 sits in the middle, 2 to its right, then up, left, left, down, down, right, right, right … the classic counter-clockwise coil. Now shade every cell whose number is prime. Ulam's shock in 1963 was that the shaded cells are not sprinkled evenly — they clump onto diagonal lines, with long dark rivers of composites in between.

    The reason is pure algebra. Because the spiral gains four cells per full turn, the numbers lying on any straight diagonal are separated by a rhythm that makes them the values of a quadratic \( P(n) = 4n^2 + bn + c \) — the \(4n^2\) is the fingerprint of "one lap = four steps." A diagonal is therefore a quadratic sequence, and every prime on the plane necessarily sits on one such diagonal. The question stops being "where are the primes?" and becomes "which quadratics are rich in primes?"

    Some are extraordinarily rich. Euler's 1772 polynomial

    \( P(n) = n^2 + n + 41 \)

    is prime for every \( n = 0,1,2,\dots,39 \) — forty primes with no gaps — and stays prime about half the time far beyond that. When you place the spiral's origin at 41, that polynomial lies exactly along the main diagonal, and the applet lights it up as an almost unbroken orange line. Its cousin \( n^2 - 79n + 1601 \) (a shift of Euler's) gives 80 primes in a row if you count \( |P(n)| \). These are not coincidences: a quadratic \( n^2+n+A \) is prime-dense precisely when the imaginary quadratic field \( \mathbb{Q}(\sqrt{1-4A}) \) has class number one, and the only such \(A\) values are 2, 3, 5, 11, 17, 41 — the celebrated Heegner numbers in disguise. The doodle is a window onto deep class-field theory.

    Why can't a diagonal be prime forever? Because \( P(0) = c \) divides \( P(c) \), \( P(2c) \), and so on — any non-constant polynomial with integer coefficients must hit composite values infinitely often. So no single quadratic sieves out all primes; the best we get is unusually long runs and unusually high densities. That's exactly what the diagonals show: not a formula for primes, but a strong statistical bias. Even choosing the origin far from 41, faint diagonal grain persists everywhere, because primes avoid the residues that share a factor with the diagonal's constant term — the streaks are the shadow of quadratic residues cast across the plane.

    What makes this a landmark of experimental mathematics is that nobody predicted it. Ulam, John Pasta, and Myron Stein saw it only because they had one of the first computers to plot it; the picture then demanded the theory. The still-open Hardy–Littlewood Conjecture F gives a precise predicted density for primes along any given quadratic, and it matches these diagonals beautifully — but a proof that even one quadratic like \( n^2+1 \) yields infinitely many primes remains out of reach. You are looking at one of the most vivid unsolved problems in mathematics, drawn as a doodle.

    Further reading: Stein, Ulam & Wells, "A Visual Display of Some Properties of the Distribution of Primes," Amer. Math. Monthly 71 (1964); Hardy & Littlewood, "Partitio Numerorum III" (Conjecture F); and the Heegner-number connection via Rabinowitsch's theorem (1913). The primes here are found by a live sieve of Eratosthenes over the whole visible range; the diagonal and polynomial overlays are computed exactly, not fitted.