Explore the Ulam Prime Spiral
A distracted mathematician sketches a mathematical classic
In 1963, mathematician Stanislaw Ulam was attending a scientific meeting. When a talk became a little dull, he began doodling on a piece of paper.
He drew a grid, wrote the number 1 in the center, and then filled in 2, 3, 4, 5... one number at a time, spiraling outward.
Then he circled the prime numbers. To his surprise, these numbers—which seemed to have no pattern at all—lined up in clear diagonal lines!
Prime numbers are often considered some of the most elusive numbers in mathematics. Their positions on the number line look haphazard, but when Ulam arranged them in a different way, a hidden structure suddenly appeared.
The image later became known as the Ulam spiral and appeared on the cover of the March 1964 issue of Scientific American. A casual doodle had become one of the best-known images in mathematical history.
First, what is a prime number?
A prime number is an integer greater than 1 that is divisible only by 1 and itself. Examples include 2, 3, 5, 7, 11, and 13.
But 6 is not prime, because 6 = 2 × 3. Besides 1 and 6, it is also divisible by 2 and 3.
Prime numbers are like the basic building blocks of the integers: every integer greater than 1 can be written as a product of primes. For example:
12 = 2 × 2 × 3
30 = 2 × 3 × 5
This is called prime factorization. And apart from the order of the factors, every integer greater than 1 has exactly one prime factorization.
Yet the positions of primes on the number line seem to follow no obvious pattern: sometimes two primes are close together, and sometimes a long gap separates them.
So what might we find if we stop arranging numbers from smallest to largest and instead lay them out in a spiral?
What happens when we arrange numbers in a spiral?
The method is simple: put 1 in the center, then place numbers one cell at a time in the directions right, up, left, and down, spiraling outward.
Once the numbers are in place, light up only the primes and leave the others blank. Each lit point represents a prime number.
The image you see now is an Ulam prime spiral.
On the ordinary number line, primes do not seem to follow any clear arrangement. But in this image, you can spot some striking diagonal lines.
That is what makes the Ulam spiral so interesting: the same set of numbers reveals mathematical structures that are hard to notice simply by changing how they are arranged.
A curious pattern: why do primes line up diagonally?
If primes were scattered completely at random, arranging them in a spiral would look more like sand thrown across a surface, with bright points everywhere.
Instead, you can see many clear diagonal lines. Some are packed with bright points, while others are almost completely empty.
That is because, although the spiral looks like it winds around arbitrarily, numbers along certain fixed directions follow precise mathematical rules.
For example, in this standard square spiral, the diagonal running up and to the right from the center contains the numbers:
1, 3, 13, 31, 57, 91...
They do not appear at random. They can be calculated using this formula:
4n² - 2n + 1
For n = 0, 1, 2, 3, and 4, the formula gives 1, 3, 13, 31, and 57, respectively.
This tells us that as we move along this diagonal, the numbers grow according to a fixed quadratic formula. Other diagonals follow similar rules, though their formulas differ.
Why does a quadratic formula appear? Each time the spiral expands by one turn, its side length increases. Along a fixed direction, the gaps between numbers change as well. Accumulating these growing gaps produces a squared term.
So the primes do not arrange themselves into diagonal lines; the way numbers are placed in the spiral makes certain mathematical formulas visible as geometric shapes.
Ulam and his colleagues later used a computer to extend the spiral to about 65,000 numbers, and the diagonal structures remained clear. This showed that the pattern was not just a coincidence among a few small numbers.
Why are some diagonals bright and others nearly empty?
If each diagonal corresponds to a mathematical formula, why do some contain many primes while others have almost no bright points? The answer involves one of the most basic properties of primes: divisibility.
Here is a simple example: except for 2, every even number is not prime because it is divisible by 2.
In this square spiral, numbers along the same diagonal have a fixed parity: some diagonals contain only odd numbers, while others contain only even numbers. So, except for the number 2 itself if it appears, the even-number diagonals are blank.
That explains some of the empty diagonals in the image.
But odd numbers are not necessarily prime. An odd number can still be a multiple of 3, 5, 7, or another prime.
For example, if the numbers along a diagonal are often divisible by 3, then none of those numbers can be prime, except 3 itself.
Conversely, if a formula happens to avoid multiples of small primes such as 2, 3, 5, and 7 over a range, it has a chance of producing more primes, and its diagonal will look brighter.
It is a little like a sieve with layer after layer of filters: first remove multiples of 2, then multiples of 3, 5, and so on. Only the numbers left at the end can be prime.
However, avoiding multiples of small primes does not guarantee that a number is prime. A larger prime may still divide it.
The brightness of a diagonal therefore reflects an interesting relationship: different mathematical formulas can vary greatly in how often they produce primes.
A particularly bright line: Euler's lucky formula
In the 18th century, the mathematician Euler discovered a fascinating quadratic formula:
n² + n + 41
For n = 0, 1, 2... all the way through 39, the formula produces 40 numbers, and every one is prime.
For example:
n = 0: 41
n = 1: 43
n = 2: 47
n = 3: 53
Producing primes 40 times in a row is a beautiful result for a quadratic formula, so it is often called Euler's lucky formula.
Why can it produce so many primes in a row? We can look at divisibility to find out.
Treating 41 as the modulus in the formula, we get:
n² + n + 41 ≡ n(n + 1) (mod 41)
This means that when n is a multiple of 41, or n + 1 is a multiple of 41, the result is divisible by 41.
For example, when n = 40:
40² + 40 + 41 = 1681 = 41 × 41
And so the run of primes comes to an end.
If you set the spiral's starting number to 41, you can observe the particularly bright diagonal associated with this formula. You can also click “Next” below the image to look for Euler's lucky line.
Keep in mind, though, that the sequence from Euler's formula corresponds to a particular direction and position in the spiral. Changing the spiral's starting point can make this kind of structure easier to see.
This example teaches us an important lesson: a formula can produce primes for a long run without producing primes forever.
A different arrangement: the Sacks spiral
The Ulam spiral is not the only way to look at primes.
In 1994, mathematician Robert Sacks introduced another interesting arrangement, called the Sacks spiral.
Unlike the Ulam spiral, the Sacks spiral does not place numbers on a grid. Instead, the numbers follow a continuous circular spiral.
It uses a special coordinate mapping that makes some mathematical patterns in the number sequence appear as curved lines in the image.
When you switch the display to the circular spiral, you can see some of the straight structures from the square spiral change.
This does not mean the primes themselves have changed. The numbers are the same, and so are the primes; only the way we look at them is different.
That is one of the most interesting things about mathematical visualization: changing our point of view can reveal new structures hidden in the data.
So, have we found a pattern in the primes?
The answer is: we have found many patterns, but we are still far from fully understanding the primes.
The Ulam spiral shows us that some quadratic formulas can produce many primes in a row. But it does not tell us whether an arbitrary formula will produce infinitely many primes.
For example, mathematicians still cannot prove whether n² + 1 takes prime values infinitely often. This is one of the famous Landau problems.
Another well-known open problem is the twin prime conjecture: are there infinitely many pairs of primes that differ by just 2? Examples include 11 and 13, 17 and 19, and 29 and 31.
We know that there are infinitely many primes, and we can study their distribution overall. For example, as numbers get larger, the proportion of primes tends to decrease.
But for any particular number, it is still difficult to predict exactly where the next prime will be using these patterns alone.
The Ulam spiral does not show that the primes have been completely decoded. It reveals something more intriguing: the seemingly chaotic distribution of primes really does contain structures that can be described by mathematical formulas.
A simple spiral image has led us to some questions in number theory that remain unanswered to this day.
Try it yourself: find your own prime patterns
Now you can adjust the settings above and explore different prime patterns for yourself. Beyond admiring the image, try these small experiments.
Experiment 1: Find Euler's lucky line
Set the starting number to 41 and see whether a particularly bright diagonal appears. You can also change the starting number to look for other diagonals with many bright points. Do these diagonals correspond to some mathematical formula?
Experiment 2: Find the empty diagonals
Look closely at the diagonals with almost no bright points. Are they related to even numbers? What happens if the numbers along a diagonal are often multiples of a small prime?
Experiment 3: Observe primes among larger numbers
Set the starting number to 1000000 and see how the prime distribution in that range compares with the one starting at 1. As the numbers get larger, do the bright points become sparser overall? Note that density can still vary significantly within a local range.
Starting number: the number at the center of the spiral. Try 1, 41, 17, or 1000000 to observe prime distributions in different ranges.
Count: how many numbers to draw, in thousands. A larger count reveals more of the overall structure, but drawing and displaying it may take longer.
Shape: Ulam is a square spiral, while Sacks is a circular spiral. Switch between them to see how straight lines become curves.
Color: the color mode applies a gradient from the center outward according to the numbers' values, helping you see how the spiral expands turn by turn.
You can also drag the image with your mouse and zoom with the scroll wheel to examine individual points up close.
Try asking your own question, then adjust the settings to look for an answer. You may discover an unexpected pattern.
Keep exploring the world of mathematics
The Ulam spiral reminds us that sometimes a new way of arranging numbers lets us see what was hidden before.
Mathematics is more than formulas and calculations; it is also a way of observing the world. A simple rule can generate a complex and beautiful pattern, and an apparently accidental pattern may hide a deep mathematical structure.
If you enjoy the way simple rules can generate complex shapes, explore more mathematical works: Julia set, Mandelbrot set, and Newton fractal.
You can also browse more fascinating mathematical shapes, or read about the Ulam spiral on Wikipedia.
Further Reading
- Ulam Prime Spiral A geometric perspective: Why do prime numbers form diagonal lines?
- Prime Number Theorem A statistical perspective: How does the number of primes grow?
- Euler Product An algebraic perspective: How can prime numbers be used to construct the sum of the reciprocal powers of all integers?
- Zeros at Work A complex analysis view: how do the zeros of the zeta function affect the distribution of primes?