← Iris
Numbers:0 Primes:0 Density:0% Render:0ms

Visualization

Mode
Highlight
Color scheme

Parameters

Grid size 200
Start number
Dot size 2

Presets

What is the Ulam spiral?

In 1963, the mathematician Stanislaw Ulam was sitting in a meeting, bored, and began doodling. He wrote the integers in a spiral pattern starting from 1 at the center, then circled the primes. To his surprise, the primes weren’t scattered randomly — they formed visible diagonal lines.

The pattern is simple: write 1 in the center of a grid. Move right to 2, up to 3, left to 4 and 5, down to 6 and 7, right to 8, 9, and 10 — spiraling counter-clockwise. Then color every cell whose number is prime. At small scales the diagonals are faint, but zoom out to hundreds of thousands of numbers and the lines become unmistakable.

Ulam published the spiral in 1964, and Martin Gardner popularized it in Scientific American. It remains one of the most visually striking unsolved patterns in number theory.

Why do primes form diagonals?

The diagonals in the Ulam spiral correspond to quadratic polynomials. When integers are arranged in a spiral, the values along any diagonal can be expressed as f(n) = an² + bn + c for specific constants a, b, c. Some of these quadratics produce an unusually high density of primes.

The most famous example is Euler’s prime-generating polynomial:

f(n) = n² + n + 41

This formula produces primes for every integer from n = 0 to n = 39 — an extraordinary run of 40 consecutive prime outputs. On the Ulam spiral, the values of this polynomial trace a diagonal line densely packed with primes.

The Hardy–Littlewood conjecture (specifically Conjecture F, from their 1923 paper) predicts the density of primes generated by quadratic polynomials. It says that the number of primes of the form an² + bn + c up to N is approximately:

C · √N / ln(N)

where C depends on the discriminant b² − 4ac. When the discriminant is small and negative, C tends to be large — meaning more primes, and more visible diagonals. This explains why some diagonals are brighter than others, but the conjecture itself remains unproven.

The prime number theorem

The prime number theorem, proved independently by Hadamard and de la Vallée-Poussin in 1896, tells us that the number of primes up to N is approximately N / ln(N). This means primes become rarer as numbers grow, but they never stop appearing entirely.

More precisely, if π(N) counts the primes up to N:

π(N) ~ N / ln(N) as N → ∞

The probability that a random number near N is prime is roughly 1 / ln(N). At N = 100, about 1 in 5 numbers are prime. At N = 1,000,000, it drops to about 1 in 14. This gradual thinning is visible in the spiral — the outer regions are sparser than the center.

Twin primes and prime gaps

Twin primes are pairs of primes that differ by 2: (3, 5), (5, 7), (11, 13), (17, 19), (29, 31), and so on. The twin prime conjecture, one of the oldest open problems in mathematics, asserts that there are infinitely many such pairs. It remains unproven, though Yitang Zhang’s 2013 breakthrough showed there are infinitely many prime pairs with gap at most 70 million — a bound that the Polymath project subsequently shrank to 246.

A prime gap is the difference between consecutive primes. The first few gaps are 1 (between 2 and 3), then 2, 2, 4, 2, 4, 2, 4, 6, ... Prime gaps grow on average like ln(p), but occasionally they spike. The longest known gaps are enormous — the current record (as of 2025) exceeds 1500 digits.

In the “prime gap” color mode, large gaps appear as darker regions — “prime deserts” where no primes exist. Try starting from a large number to see these deserts emerge.

Prime-generating polynomials

Beyond Euler’s celebrated n² + n + 41, many quadratic polynomials generate primes at rates exceeding what random chance predicts. A few famous examples:

n² + n + 41 — primes for n = 0..39 (Euler, 1772) 2n² + 29 — primes for n = 0..28 (Legendre) n² - n + 41 — primes for n = 0..40 (equivalent to Euler’s) 6n² + 6n + 31 — primes for n = 0..28

By Dirichlet’s theorem, no polynomial can produce only primes forever. But some come remarkably close over large ranges, and it is these polynomials that create the conspicuous diagonals in the Ulam spiral. The Bunyakovsky conjecture generalizes the question: does every irreducible polynomial with positive leading coefficient take infinitely many prime values? This too remains open.