The Ulam spiral

The Ulam spiral is a picture of the primes. Write the positive integers in a square spiral, mark the primes, and they do not look like noise. They gather on diagonal, horizontal, and vertical lines, and those lines are quadratic polynomials.

Ulam spiral of primes, with Euler's diagonal of forty primes drawn through the center
Ulam spiral of primes, with Euler's diagonal of forty primes drawn through the center

A doodle from 1963

Stanisław Ulam found the spiral in 1963, during what Martin Gardner later called a long and very boring paper at a scientific meeting. Ulam wrote the positive integers in a spiral on a square grid and circled the primes. The circles lined up. He, Myron Stein, and Mark Wells then ran the picture out to about 100,000 points on the MANIAC II computer at Los Alamos. Gardner's March 1964 Mathematical Games column in Scientific American put the spiral on the cover. The lines are not a proof of how the primes are spaced. They are a way to see a fact that number theory already knew in another form: some quadratic polynomials produce primes much more often than others.

How the square is filled

The spiral starts at 1 in the center. The next number is one step to the right, and the path then turns left, counterclockwise, with each straight leg one step longer than the last: one step, one step, two, two, three, three, and so on. The first complete square reads, row by row: 17 16 15 14 13, then 18 5 4 3 12, then 19 6 1 2 11, then 20 7 8 9 10, then 21 22 23 24 25. You can start at a number other than 1. The same sort of lines appear. Starting at 41 is the famous case.

Lines are quadratic polynomials

Every diagonal, and every horizontal or vertical ray, is exactly the set of values of a quadratic f(n) = 4n² + bn + c. When b is even the line is diagonal, and the values are all odd or all even, depending on c. Every prime except 2 is odd, so the primes sit on every other diagonal. Some of those quadratics factor. 4n² + 8n + 3 = (2n + 1)(2n + 3), so that diagonal is almost empty of primes. Others stay prime surprisingly often. The difference is not magic. It is how often the polynomial is forced to be divisible by a small prime. Hardy and Littlewood's Conjecture F, a special case of the Bateman–Horn conjecture, predicts the density along ax² + bx + c. The density depends on the discriminant b² − 4ac. No quadratic has been proved to produce infinitely many primes. That is one of Landau's problems, and it is still open.

Euler's forty primes

Euler's polynomial n² + n + 41 is prime for every n from 0 through 39. That is forty primes. At n = 40 it gives 1,681 = 41², and the run stops. On the spiral those values are the single quadratic 4n² − 2n + 41, which is Euler's polynomial at the even integers, together with the opposite direction 4n² + 2n + 41. They become one straight diagonal when the cell in the center is 41 rather than 1. The unbroken run is the forty values from 1,523 (nineteen steps southwest of the center) through 41 to 1,601 (twenty steps northeast). The next cells, 1,681 and 1,763, are composite, so the gold line breaks. Hardy and Littlewood's constant for 4n² − 2n + 41 is about 6.6: the conjecture says these values are prime nearly seven times as often as random numbers of the same size.

What the picture is for

The spiral does not decide the prime-number theorem, and a long diagonal is not a proof. It is a map. Prime-rich and prime-poor quadratics are visible at a glance, which is why Ulam, and Laurence Klauber before him in 1932, bothered to draw them. Klauber used a triangle whose rows run from one square to the next. Robert Sacks later put the integers on an Archimedean spiral so that one square falls on each turn; there Euler's polynomial is a curve rather than two diagonal rays. In the square spiral the thing to look at is the formula of the line under your finger, how many primes it holds, and how that compares with 1 / ln n, the chance a random number of that size is prime.

Explore it in PictorX

Open the Ulam spiral and you get the classic picture: a 101×101 square, primes in gold, composites hidden. Drag to move and scroll to zoom. The 5×5 favorite writes every number, so you can check the path against the square above. Euler's 40 starts the spiral at 41 and draws 4n² − 2n + 41. Richest line searches both diagonal directions for the one with the most primes. Click any cell to put its diagonal in the readout, with the polynomial, the longest prime run, and the comparison with a random number of that size. Residue mod 4 and last digit recolor the primes. Save a PNG or open the canvas in the PictorX editor. Pair it with the Collatz Conjecture or Langton's Ant for more simple rules with a surprising picture.

Frequently asked questions

Who discovered the Ulam spiral?

Stanisław Ulam, in 1963, while doodling during a scientific meeting. Martin Gardner popularized it in Scientific American in March 1964. Laurence Klauber had drawn a related triangular array of primes in 1932.

Why do the primes line up?

A diagonal, row, or column of the square spiral is a quadratic polynomial 4n² + bn + c. Some of those polynomials are divisible by a small prime unusually often, and some almost never are. The primes are what is left.

What is special about n² + n + 41?

It is prime for n = 0, 1, 2, …, 39, forty primes, and composite at n = 40 because the value is 41². On the spiral, start at 41 and that run is one diagonal: 1,523 through 41 to 1,601, the values of 4n² − 2n + 41.

Does the spiral prove how the primes are distributed?

No. It shows the variation that Hardy and Littlewood's Conjecture F tries to predict. No quadratic polynomial has been proved to generate infinitely many primes.

Ready to try it?

Open PictorX in your browser — no install, no credit card required to start.

Try the Ulam spiral