Prime Spiral Explorer

Prime Spiral Explorer

An interactive playground for seeing number theory: 100,000 integers laid out on spiral grids, with classifications, comparisons, and curve-fitting overlays that turn famous theorems into things you can point at. Primes glow amber; whatever you compare them against glows cyan; the one number that belongs to both gets a white halo. Everything below is running live at primes.charliedeck.com.

A dark field of 100,000 numbers as glowing dots, primes in amber and triangular numbers in cyan, with a classification panel
Primes (amber) against triangular numbers (cyan) across the first 100,000 integers. Exactly one dot is both.

A Bored Doodle at Los Alamos

In 1963, Stanisław Ulam, the Polish-American mathematician behind the Monte Carlo method and half of the Teller–Ulam thermonuclear design, sat through what he later described as a long and very boring talk. He did what mathematicians do in boring talks: he doodled. He wrote 1 in the middle of a grid, spiraled the integers outward, and started circling the primes.

What he saw made the talk considerably less boring. The circled numbers weren't scattered like static. They clumped along diagonal lines: long, conspicuous streaks that had no business being there if primes were as patternless as they're reputed to be. Ulam, with Myron Stein and Mark Wells, put the question to MANIAC II at Los Alamos and plotted primes into the tens of thousands. The streaks persisted. Martin Gardner ran the pictures in his Mathematical Games column in March 1964, the spiral made the cover of Scientific American, and the Ulam spiral has been reprinted endlessly since.

This app is that doodle, industrialized: a hundred thousand integers on the spiral, live classification overlays, motion between layouts, and the tools to interrogate the streaks.

Why Diagonals? Because Diagonals Are Quadratics

The Ulam spiral's geometry is secretly algebraic.

Walk out from the center along any fixed diagonal ray and write down the numbers you land on. Each full lap of the spiral is a square ring, and each ring's perimeter is eight steps longer than the last, so the gaps between your successive numbers grow by a constant 8. A sequence whose second difference is constant is a quadratic. Every diagonal (and every horizontal and vertical line) of the spiral is exactly the values of some polynomial

f(n)=4n2+bn+cf(n) = 4n^2 + bn + c

for integer b,cb, c. You can check the most famous case by eye: the odd squares 1,9,25,49,81,1, 9, 25, 49, 81, \ldots, which is to say (2n+1)2(2n+1)^2, march out along a single southeast ray.

So Ulam's observation translates cleanly out of geometry: a bright diagonal is a quadratic polynomial that hits primes far more often than it has any right to. The spiral is a device that evaluates every such quadratic at once and lets your visual cortex spot the rich ones. The app's Quadratic Fitting panel lets you run this backwards: pick dots along a streak and it recovers the polynomial through them, so the line you noticed becomes a formula you can read.

Why Some Quadratics Are Prime-Rich

Now the deep part. Three facts stack up:

Primes thin out on a precise schedule. The Prime Number Theorem says that near NN, about 11 in lnN\ln N numbers is prime. If primes were otherwise "random" with that density (the useful fiction behind most prime heuristics), a typical quadratic's values near NN should be prime about as often as any other number of that size.

But quadratics aren't typical numbers; they have congruence habits. A polynomial's values follow fixed patterns modulo every small prime. Consider Euler's celebrated

f(n)=n2+n+41,f(n) = n^2 + n + 41,

which is prime for every nn from 0 to 39. Note that n2+n=n(n+1)n^2 + n = n(n+1) is always even, so f(n)f(n) is always odd: this quadratic has opted out of divisibility by 2 entirely, which doubles its prime odds relative to a random number. Work out its behavior mod 3, mod 5, mod 7, and it dodges those too. Each small prime a quadratic avoids multiplies its prime density by another factor bigger than 1. (Why 41 specifically works so well is one of the loveliest facts in algebraic number theory: its discriminant is 1441=1631 - 4 \cdot 41 = -163, the largest of the nine Heegner numbers, and Rabinowitsch's theorem ties the "prime for n=0,,q2n = 0, \ldots, q-2" property to class number one. The magic of 41 and the magic of eπ163e^{\pi\sqrt{163}} being freakishly close to an integer are the same magic.)

Hardy and Littlewood quantified the whole picture, conjecturally. Their 1923 Conjecture F predicts that each quadratic hits primes at the random-model rate scaled by a computable constant: the product, over all primes pp, of how strongly the polynomial dodges or courts divisibility by pp. Bright Ulam streaks are precisely the quadratics with large Hardy–Littlewood constants, and Euler's polynomial (which lives on a diagonal of a spiral centered at 41) is among the brightest known.

The humbling footnote: all of this is still conjecture. After a century, no one has proved that any quadratic polynomial takes infinitely many prime values. n2+1n^2 + 1 is Landau's problem, still open. The diagonal streaks you can see in two seconds in the app sit just past the edge of what mathematics can currently prove. (For contrast, the linear case, arithmetic progressions like 4n+34n+3, was settled by Dirichlet in 1837, and his theorem is doing quiet work in every layout the app draws.)

Reading the App Like a Census

The layout is pure arithmetic, so everything you see is a checkable claim. The Classification panel colors any family of numbers and tallies exact counts; Compare With overlays a second family and reports the overlap. The screenshot above catches my favorite tally:

  • 9,592 of 100,000 numbers are prime, and indeed π(100,000)=9,592\pi(100{,}000) = 9{,}592, the true count.
  • Against triangular numbers, the overlap reads: primes only 9,591, triangular only 445, both 1.

That lone white-haloed dot is 3, the only number that is both prime and triangular. It's a two-line proof (every later triangular number n(n+1)2\tfrac{n(n+1)}{2} inherits a factor from nn or n+1n+1), but watching a hundred-thousand-dot field produce exactly one white dot makes the theorem feel like a census result. Hovering any dot reports its nature and prime factorization.

The Other Prime Spiral

If this itch is worth scratching further, Grant Sanderson's 3Blue1Brown video is the essential companion piece. It starts from a different construction (plot each prime at polar coordinates (p,p)(p, p), radius and angle both equal to the prime) and watches galaxy-arm spirals emerge, then dissolve into rays at larger scales:

The resolution is a beautiful cousin of the Ulam story: the polar spirals come from rational approximations of 2π2\pi (44 arms from 77 turns, 710 arms beyond) crossed with which residue classes can contain primes at all. Dirichlet's theorem again. The moral carries over: when primes appear to organize themselves, you've usually caught modular arithmetic in the act.

Small Tool, Old Questions

None of this is research. But there is a real distance between "heard about the Ulam spiral" and "watched n2+n+41n^2 + n + 41 light up under my cursor," and closing it is what a toy like this is for. Load it up, switch the comparison families, and fit a quadratic to the boldest streak you can find. If you can prove what you're seeing, you'll be the first.

Further Reading

  • Stein, Ulam & Wells (1964). "A Visual Display of Some Properties of the Distribution of Primes." American Mathematical Monthly
  • Gardner, M. (March 1964). "Mathematical Games." Scientific American -- the column that made the spiral famous
  • Hardy & Littlewood (1923). "Some problems of 'Partitio Numerorum' III" -- Conjecture F
  • 3Blue1Brown: Why do prime numbers make these spirals?