Number Theory

The Ulam Spiral: Hidden Lines in the Prime Numbers

The Ulam Spiral is what you get when you write the whole numbers 1, 2, 3, 4, … in a square spiral winding outward from the center and then color in only the primes. Instead of scattering like static, the primes crowd onto diagonal streaks that stripe the whole picture. Those streaks are not an illusion: each one traces the values of a quadratic polynomial like Euler's famous n² + n + 41, and their brightness is governed by deep number theory — class numbers, quadratic reciprocity, and the still-unproven Hardy–Littlewood conjectures on how densely primes hide inside quadratics.

  • Discovered1963, Stanisław Ulam (doodling in a lecture)
  • Made famousScientific American cover, March 1964
  • Brightest linen² + n + 41, prime for n = 0…39
  • Diagonal polynomials4n² + bn + c (leading coefficient 4)
  • Euler discriminant−163, a Heegner number (class number 1)
  • Density decay along a line≈ 1 / log N (only logarithmic)

Interactive visualization

Press play, or step through manually. The visualization is yours to drive — try it before reading on.

Open visualization fullscreen ↗

Watch the 60-second explainer

A condensed visual walkthrough — narrated, captioned, under a minute.

Building the spiral

Start with 1 at the center of a grid. Step right to 2, up to 3, left to 4 and 5, down to 6 and 7, and keep winding counter-clockwise: each loop adds one ring to a square spiral that fills the entire plane with the positive integers in order. Now erase every composite number and leave only the primes lit.

If primes fell at random you would expect a featureless gray fog, thinning slowly outward (by the Prime Number Theorem, the density of primes near x is about 1/log x). Instead the eye immediately catches diagonal lines — long slanted runs of lit cells — together with fainter horizontal and vertical ones. The pattern survives even if you start the count at some number other than 1, and it only sharpens as you plot millions of integers. That refusal to look random is the whole phenomenon, and it has a precise explanation.

Every line is a quadratic

The secret is the geometry of the spiral itself. Walk along any straight diagonal (or row, or column) and read off the numbers you pass; they always form a quadratic sequence. Concretely, the values on a diagonal obey a polynomial of the form 4n² + bn + c for fixed integers b and c.

Why the constant 4? Each time you step one ring outward along a fixed direction, the loop you just completed added roughly 8 times the ring number to the running count, so the numbers on the line increase with a constant second difference of 8. Since a quadratic an² + … has second difference 2a, we get 2a = 8, hence a = 4. The perfect squares make this vivid: the even squares (2n)² = 4n² lie on one main diagonal, and the odd squares (2n+1)² = 4n² + 4n + 1 lie on the other. Every diagonal parallel to these is just another 4n² + bn + c.

This already forces a coarse striping. Half of these quadratics take only even values (their constant term is even), so apart from the isolated prime 2 they are entirely composite — dark diagonals. Primes can only live on the odd-valued lines, so before any deep theory the spiral is pre-sorted into candidate lines and forbidden lines. The real question is why some of the candidate lines glow so much brighter than others.

Euler's polynomial and the brightest streak

The most spectacular line is the one carrying Euler's polynomial, f(n) = n² + n + 41. Leonhard Euler noticed in 1772 that it is prime for every value from n = 0 to n = 39 — forty primes in a row: 41, 43, 47, 53, 61, 71, …, 1601. The streak finally breaks at n = 40, where f(40) = 1681 = 41² (and it must, since f(41 − 1) is divisible by 41).

On the spiral this polynomial does not sit on a single diagonal but splits across two, because its even-index and odd-index values are each leading-coefficient-4 quadratics — for instance the even terms are 4m² + 2m + 41 (41, 47, 61, 83, …). Recenter the spiral so that 41 sits near the middle and a strikingly long, nearly unbroken diagonal of primes leaps out. Euler's polynomial is not a curiosity: it is the single most prime-dense quadratic known of its kind, and it is the reason the Ulam spiral has a headline result rather than a mild statistical wobble.

Why 41? Class numbers and the Heegner miracle

The precise reason Euler's polynomial runs so long is a jewel of algebraic number theory. In 1913 Georg Rabinowitz proved: n² + n + p is prime for all n from 0 to p − 2 if and only if 1 − 4p is squarefree and the imaginary quadratic field ℚ(√(1 − 4p)) has class number 1 — that is, its ring of integers has unique factorization. (The squarefree clause is why p = 7 is excluded: its discriminant −27 is not squarefree — 27 = 3³ — so 3 divides f(1) = 9 even though ℚ(√−27) = ℚ(√−3) itself has class number 1.)

The fields ℚ(√d) with class number 1 are famously scarce. The Stark–Heegner theorem (Heegner 1952, completed by Stark and Baker in the 1960s) proves there are exactly nine such d: the Heegner numbers 1, 2, 3, 7, 11, 19, 43, 67, and 163. Set 1 − 4p equal to the negative of a Heegner number and solve for a prime p, and you recover exactly Euler's lucky numbers 2, 3, 5, 11, 17, 41. The largest Heegner number, 163, gives p = 41 — which is precisely why 41 produces the longest possible run and why 163 is the deepest of all: 163 is a genuine hard limit, not the start of an infinite list. There is no p = 100 or p = 1000 with the same magic, because the list of unique-factorization fields simply ends.

Brightness, fixed divisors, and quadratic reciprocity

What makes one candidate diagonal brighter than another? Two conditions. First, the quadratic must be irreducible (otherwise it factors and is almost never prime). Second — and decisively — it must have no fixed prime divisor: there should be no small prime that divides every value.

Whether a prime q can ever divide a value of a quadratic with discriminant D is settled by quadratic reciprocity through the Legendre symbol (D | q). The quadratic has a root modulo q — and so q divides some of its values — exactly when D is a quadratic residue mod q. If instead (D | q) = −1, then q never divides any value at all, permanently removing one whole family of potential factors and boosting the supply of primes. A polynomial whose discriminant is a non-residue for many small primes is therefore prime-rich.

This is the same fact as the class-number miracle wearing different clothes: the class-number-1 condition for n² + n + 41 is equivalent to saying that its discriminant −163 is a quadratic non-residue modulo every prime below 41. No small prime can gain a foothold, so the values are forced to be prime for a very long stretch. The bright Ulam diagonals are exactly the quadratics that dodge the most small factors.

The density law: Hardy–Littlewood Conjecture F

How many primes should a good diagonal actually contain? The quantitative answer is Hardy and Littlewood's Conjecture F (1923). For an irreducible f(n) = an² + bn + c with a > 0 and no fixed prime divisor, the number of n up to N for which f(n) is prime is conjectured to grow like

  • #{n ≤ N : f(n) prime} ≈ C · N / log N,

where the constant C is an Euler product over odd primes, C = ∏p (1 − (D | p)/(p − 1)), built from exactly the Legendre symbols above. Primes p with (D | p) = −1 push each factor above 1 and enlarge C; that is the mechanism turning 'many small non-residues' into 'bright line.' For Euler's polynomial C is roughly 3.3, meaning its diagonal is about three times as prime-dense as a generic quadratic of the same size.

Two consequences explain the picture. Because f(n) ≈ an² has magnitude on the order of N², its 'chance' of being prime is about 1/log(N²) ≈ 1/(2 log N): the density along a line decays only logarithmically, so the streaks stay visible even millions of terms out. And the constant C, not raw size, decides which lines shine — precisely what the eye sees. Conjecture F is a special case of the broader Bateman–Horn conjecture, and, like it, remains unproven.

History, variants, and what stays open

The pattern was spotted by Stanisław Ulam in 1963 while he doodled a number grid through what he called a 'long and very boring' lecture. With Myron Stein and Mark Wells he had the MANIAC II computer plot spirals up to 65,000 numbers, and the striking image reached a wide audience on the cover of Scientific American in March 1964 alongside Martin Gardner's column. Variants followed, notably Robert Sacks's spiral (1994), which places the numbers on an Archimedean spiral so that the perfect squares fall on a single ray and the prime-rich quadratics curve into graceful arcs.

What remains unknown is humbling. The Bunyakovsky conjecture — that an irreducible polynomial with positive leading coefficient and no fixed divisor produces infinitely many primes — is unproven for every polynomial of degree ≥ 2. We cannot even prove that n² + 1 is prime infinitely often (one of Landau's four problems from 1912). So the Ulam spiral is a rare thing in mathematics: a picture a child can draw that displays, in plain sight, structure in the primes we can describe with startling precision through quadratic reciprocity and class numbers, yet still cannot fully prove.

The 'lucky numbers of Euler': prime-rich quadratics n² + n + p and why they run so long
PolynomialDiscriminant 1 − 4pClass number of ℚ(√D)Consecutive primes (n = 0 … p−2)
n² + n + 41−1631 (Heegner)40 values (n = 0…39)
n² + n + 17−671 (Heegner)16 values (n = 0…15)
n² + n + 11−431 (Heegner)10 values (n = 0…9)
n² + n + 5−191 (Heegner)4 values (n = 0…3)
n² + n + 7−27 (not squarefree)1, but 27 is not Heegner1 value — fails at n = 1 (9 = 3²)

Frequently asked questions

Is the Ulam spiral just a coincidence?

No. The diagonals are a genuine, explainable effect: every straight line in the spiral traces a quadratic 4n² + bn + c, and quadratics with the right discriminant are provably prime-rich. What is still unproven is the exact long-run density (Hardy–Littlewood Conjecture F), not the existence of the lines.

Does n² + n + 41 give primes forever?

No. It is prime for the first 40 inputs (n = 0 to 39), then fails at n = 40, where the value is 1681 = 41². It continues to produce primes unusually often afterward, but not without gaps, and no quadratic is prime for all n.

Why is the number 41 so special?

Because its discriminant is 1 − 4·41 = −163, and 163 is the largest Heegner number, the field ℚ(√−163) has unique factorization. By Rabinowitz's theorem this is exactly the condition that forces the longest possible unbroken prime run. There are only nine Heegner numbers, so the phenomenon has a hard ceiling.

Do the primes really line up, or is it wishful pattern-seeing?

They really line up in a measurable, statistical sense: prime-rich diagonals carry several times as many primes as neighboring lines, quantified by the Hardy–Littlewood constant. The lines are faint enough that the effect is best seen over thousands of cells, which is why computers made it visually dramatic.

Why do some diagonals have no primes at all?

About half of the diagonal quadratics take only even values (their constant term is even), so apart from 2 they are entirely composite. Others share a small fixed prime factor. Only quadratics that are irreducible and free of fixed prime divisors can host primes.

How is this connected to the big open problems about primes?

The density of primes along each line is exactly what Hardy–Littlewood Conjecture F and the more general Bateman–Horn conjecture try to predict, and both are unproven. Even the weaker Bunyakovsky conjecture, that a good quadratic yields infinitely many primes, is unproven for every degree-2 polynomial, including n² + 1.