Study and Productivity

The Ulam Spiral: Why Prime Numbers Line Up on Diagonals

What the Ulam and Sacks spirals show about prime numbers, why diagonals appear, which quadratics give many primes and what prime gaps and twin primes are.

Saizul Amin
Saizul Amin3 Nov 2026 · 4 min read
The Ulam Spiral: Why Prime Numbers Line Up on Diagonals

The Ulam Spiral: Why Prime Numbers Line Up on Diagonals

Prime numbers have been studied for more than two thousand years, yet simple pictures of them still surprise people. The most famous is the Ulam spiral, discovered almost by accident in 1963. This guide explains how it is built, what its patterns mean and what they do not prove, using the free Ulam spiral and prime patterns visualizer, which draws primes up to one million in your browser.

The story

The Polish-American mathematician Stanisław Ulam was sitting through a long presentation and began writing numbers on squared paper. He started with 1 in the middle and wound the numbers outwards in a square spiral, then circled the primes. To his surprise the circled numbers clustered along diagonal lines. Later, with computer help at Los Alamos, he drew tens of thousands of numbers and the lines remained. The picture reached a wide audience on the cover of Scientific American in 1964.

How to build it

  1. Write 1 in the centre.
  2. Write 2 to its right, then turn anticlockwise: 3 above 2, then 4 and 5 to the left along the top.
  3. Keep turning: 6 and 7 down the left side, 8, 9 and 10 along the bottom, and so on.
  4. Each completed ring ends on an odd square: 9, 25, 49, 81.
  5. Mark the primes.

The visualizer does this for up to a million numbers, using the sieve of Eratosthenes to find the primes. Drag to pan, scroll or press the zoom buttons, and hover to see which number sits under the pointer.

Why diagonals appear

Every straight line in the spiral, diagonal, horizontal or vertical, is a sequence of numbers given by a quadratic expression such as 4n² + 2n + 1. That happens because each ring is longer than the last by a fixed amount, so moving outwards along a line adds a steadily growing step.

Some quadratics produce primes far more often than average. Their values avoid being divisible by 2, 3, 5 and other small primes, so more of them survive. Lines full of primes are the values of such quadratics; empty lines come from quadratics whose values are always even or always factor.

Euler's famous polynomial

In 1772 Leonhard Euler noticed that n² + n + 41 is prime for every n from 0 to 39, forty primes in a row. At n = 40 it gives 1681 = 41². Another record holder, n² − 79n + 1601, gives primes for n from 0 to 79. In the visualizer, tick the highlight box and press either button: prime values turn red and composite ones yellow, and you can see Euler's values form a line through the centre region.

No polynomial produces only primes, and that is a theorem, not just an observation.

The Sacks spiral

In 1994 Robert Sacks proposed a different layout. Number n is placed at distance √n from the centre and at angle 2π√n. All perfect squares then fall on one horizontal ray, and the primes trace smooth curves leaving the centre. Some of those curves are, again, values of quadratics like n² + n + 41. Switch between the two spirals to compare them.

Prime gaps and twin primes

The gap between consecutive primes grows slowly on average. The prime number theorem says that near a large number N, the average gap is about ln N, so near one million it is about 14. The gaps tab charts how often each gap size occurs.

Up to one million:

  • there are 78 498 primes,
  • 8 169 pairs are twin primes (two apart, such as 101 and 103),
  • the largest gap is 114, from 492 113 to 492 227.

Whether there are infinitely many twin primes is one of the oldest unsolved problems in mathematics. In 2013 Yitang Zhang proved that infinitely many pairs of primes lie within a fixed bounded distance of each other, and later work brought that bound down to 246, but the twin-prime case of gap 2 is still open.

What the picture does and does not show

The spiral is a beautiful way to see that primes are not distributed like random dots: certain arithmetic patterns favour them. But a picture is not a proof. Conjectures about how many primes a quadratic produces, such as the Hardy-Littlewood conjecture F, remain unproved.

Ideas for a project

  • Compare the number of primes on two diagonals of your choice up to one million.
  • Plot the count of twin primes below N for several values of N and compare with N ÷ (ln N)².
  • Find a quadratic that gives at least 10 primes in a row and show it on the spiral.

The visualizer exports images as PNG and the gap table as CSV.

Limits

The tool draws numbers up to one million and does not test larger numbers. Highlighted patterns illustrate known results and open conjectures; they are not evidence that a conjecture is true. Prime counts were checked against the On-Line Encyclopedia of Integer Sequences.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages