Skip to content
Adrien Hubert

Ulam spiral, primes on a square coil.

Wind the integers 1, 2, 3, ... into a square spiral out from the centre. Fill in every cell that holds a prime. The result is not the wash of noise you would expect. Primes cluster along diagonal lines, and some diagonals stay dark for a hundred cells at a stretch. Stanislaw Ulam noticed the pattern on a notepad in 1963 during a talk he was not paying attention to.

Side 201
Cells 40 401
Under cursor
201
1

How the spiral is wound

Place 1 in the centre. Walk one cell right to 2, one up to 3, two left to 4 then 5, two down to 6 then 7, three right to 8, 9, 10, three up to 11, 12, 13. Steps run 1, 1, 2, 2, 3, 3, 4, 4 and never break the pattern. The direction cycles right, up, left, down. The corners of the spiral are the squares of odd numbers: 1 sits alone in the middle, 9 lands at the bottom-right of a 3 × 3 ring, 25 at the bottom-right of a 5 × 5, and so on.

Why the diagonals show up

Any diagonal in the spiral corresponds to values of a quadratic in n of the form 4n² + bn + c. Quadratics of that shape often hit long runs of primes before they start dropping composites, because they avoid small factors by construction. The most famous case is Euler's n² + n + 41, which yields a prime for every n from 0 to 39: forty consecutive primes in a straight line. Ring it with the button above and the line jumps out.

Shifting the origin

The start-number slider changes which integer sits at the centre. The spiral is the same shape, but different quadratics land on the diagonals. Try starting at 41 and the Euler diagonal slides to a different part of the frame. Try starting at 17 and a shorter Legendre-style run appears. The choice of origin is arbitrary; the fact that primes obey the shape of the spiral is not.

Sources

  • Stein, M. L., Ulam, S. M., Wells, M. B. (1964). A Visual Display of Some Properties of the Distribution of Primes. American Mathematical Monthly, 71(5), 516–520. The original write-up, with pictures.
  • Gardner, M. (1964). Mathematical Games: The remarkable lore of prime numbers. Scientific American, 210(3), 120–128. How the spiral reached a wider audience.
  • Ribenboim, P. (1996). The New Book of Prime Number Records. Springer. Chapter 3 on prime-generating polynomials, including Euler's n² + n + 41.