What is the Ulam Spiral?

The Ulam Spiral is an interesting way to visualize prime numbers, revealing surprising patterns within their distribution. Its discovery came unexpectedly in 1963 when Polish mathematician Stanisław Ulam, during a rather dull lecture, began filling a spiral grid in his notebook with natural numbers in a counterclockwise sequence:

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849

Then, he circled the prime numbers:

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849

To his surprise, the circled primes formed distinct patterns. Clusters were appearing along certain diagonal lines, revealing a striking non-random structure. When extended to a 101x101 grid, with primes marked as black points, the pattern becomes even more pronounced:

Ulam Spiral on a 101 by 101 grid, with primes marked as black points

Want a closer look? Open this spiral in the generator and zoom in until the numbers appear.

Why do primes line up?

The spiral alternates between odd and even numbers like a checkerboard, so every diagonal is entirely odd or entirely even. Apart from 2, primes can only appear on the "odd" diagonals.

Each diagonal also follows a quadratic formula of the form 4n² + bn + c. Some of these formulas avoid multiples of small primes and produce primes unusually often, while others produce very few, which is why some lines stand out so clearly from their neighbors.

The most famous example is Euler's n² + n + 41, which is prime for every n from 0 to 39. Those first 40 primes curl around the center of the spiral. From n = 40 onward, every value lands on one of the two highlighted diagonals, 40 columns to either side of the diagonal through the center. In this 151x151 view, 82 of those 111 values are prime, compared with about 22% of all odd numbers. The Hardy-Littlewood Conjecture F predicts how prime-rich each formula should be, but it remains unproven.

Ulam Spiral on a 151 by 151 grid with the values of n² + n + 41 highlighted, forming a cluster at the center and two diagonal lines running up-right and down-left

For a deeper dive into this topic, take a look at this excellent video from Numberphile:

About This Website

This is a pastime project aiming to popularize the concept of the Ulam Spiral. When I first learned about the spiral I was hypnotized by the astounding beauty of the prime distribution. I developed the generator and made it public. It runs in your browser, using a JavaScript port of my open-source Java library, which can also compute much larger spirals.

Contact Form

Just in case you would need to get in touch with me.