Ulam Spiral Explained: Why Primes Form Diagonals
Quick answer: The Ulam spiral is made by writing the whole numbers 1, 2, 3, … in a square spiral and marking the primes. Surprisingly, many primes cluster along diagonal lines. Mathematician Stanisław Ulam noticed it in 1963 while doodling during a boring talk. Each diagonal ray follows a quadratic formula, such as 4r² − 2r + 1 for the northeast ray (3, 13, 31, 57, …), and some of these quadratics produce unusually many primes. The pattern is real, but it does not give a formula for all primes.
In 1963, the mathematician Stanisław Ulam was stuck in a long, dull presentation at a scientific meeting. To pass the time, he wrote the numbers 1, 2, 3, … on a grid in a spiral and circled the primes. To his surprise, the primes seemed to line up along diagonals. The picture, now called the Ulam spiral, became famous after it appeared on the cover of Scientific American in 1964.
How to Draw It
Put 1 in the center. Write 2 to its right, then spiral counterclockwise: 3 above 2, 4 and 5 to the left, and so on. Then highlight the prime numbers.
| 17 | 16 | 15 | 14 | 13 |
| 18 | 5 | 4 | 3 | 12 |
| 19 | 6 | 1 | 2 | 11 |
| 20 | 7 | 8 | 9 | 10 |
| 21 | 22 | 23 | 24 | 25 |
Even in this tiny grid, primes such as 3 and 13, 5 and 17, and 7 and 19 already line up along diagonals. Check numbers yourself with our prime number checker.
Why Diagonals?
Numbers along a diagonal line of the spiral are given by a quadratic polynomial. Here are the four rays leaving the center 1, with \(r = 1, 2, 3, \dots\) counting steps from the center:
| Ray | Numbers | Formula | Primes? |
|---|---|---|---|
| Northeast | 3, 13, 31, 57, 91, … | \(4r^2 – 2r + 1\) | Many |
| Northwest | 5, 17, 37, 65, 101, … | \(4r^2 + 1\) | Many |
| Southwest | 7, 21, 43, 73, 111, … | \(4r^2 + 2r + 1\) | Many |
| Southeast | 9, 25, 49, 81, 121, … | \((2r + 1)^2\) | Never: these are odd squares |
Other diagonals, which do not pass through the center, also follow quadratics; for example 7, 19, 39, 67, 103, … lies on a diagonal one step off the center. Since the spiral alternates between odd and even numbers in a checkerboard pattern, every diagonal is all odd or all even, which already explains why half the diagonals are empty. Beyond that, some quadratics are “prime-rich”: their values are never divisible by small primes like 3, 5 or 7, so more of them are prime.
A famous example is Euler’s polynomial
\[ n^2 + n + 41, \]
which is prime for every \(n\) from 0 to 39 (giving 41, 43, 47, …, 1601), and first fails at \(n = 40\), where it gives \(1681 = 41^2\). If you start the spiral at 41 instead of 1, these 40 primes all land on one diagonal line through the center, alternating between the two halves of the line: the odd-numbered values on one side (\(4r^2 – 2r + 41\)) and the even-numbered values on the other (\(4r^2 + 2r + 41\)).
What the Pattern Does and Does Not Show
- Real: some diagonals contain noticeably more primes than others, explained by which small primes their polynomial avoids.
- Conjectured: the Bunyakovsky conjecture predicts that suitable quadratics produce infinitely many primes, and the Hardy–Littlewood “Conjecture F” (1923) predicts how often. They match the data but are unproved. Even whether \(n^2 + 1\) is prime infinitely often is unknown; it is one of Landau’s four problems, alongside Legendre’s conjecture (a different question, about primes between consecutive squares).
- Not a prime formula: no non-constant polynomial with integer coefficients gives a prime for every whole-number input, and the spiral does not predict where every prime lies.
Similar patterns appear in other spirals, such as the Sacks spiral and the polar plots explained by 3Blue1Brown (see the video below).
Common Misconceptions
“The spiral reveals a secret formula for primes.” No. It reveals biases in quadratic polynomials, not a rule for all primes.
“The pattern is just an illusion.” Partly no: the excess of primes on certain diagonals is real and predicted by number theory conjectures that fit the data extremely well.
Watch: The Ulam Spiral in Video
If you learn better by watching, these videos are worth your time.
Check Your Understanding
Five quick questions to see how much of this has stuck. Pick an answer and press Submit; you will see a short explanation either way.
Answer each question and press Submit to see your result right away.
Frequently Asked Questions
What is the Ulam spiral?
A square spiral of the whole numbers with the primes marked, which shows primes clustering along diagonal lines.
Why do primes form diagonal lines in the Ulam spiral?
Each diagonal corresponds to a quadratic polynomial. Diagonals are all odd or all even, and some odd quadratics also avoid small prime factors like 3, 5 and 7, so their values are prime more often.
What formula gives a diagonal of the Ulam spiral?
The rays from the center 1 are 4r² − 2r + 1 (northeast), 4r² + 1 (northwest), 4r² + 2r + 1 (southwest) and (2r + 1)² (southeast), for r = 1, 2, 3, ….
Who discovered the Ulam spiral?
Stanisław Ulam in 1963; it was popularized by Martin Gardner and a 1964 Scientific American cover.
Does the Ulam spiral give a formula for primes?
No. It reveals biases in certain polynomials, but there is no simple formula that produces all primes.
Keep Exploring
- Legendre’s Conjecture: A Prime Between Every Two Squares?
- Prime Number Theorem Explained: Formula and Examples
- Mersenne Primes: List of All 52 Known (2026)
Sources and Further Reading
Related to This Article
More math articles
- Low-Temperature Sterilization Methods
- Full-Length SSAT Middle Level Practice Test-Answers and Explanations
- FREE 4th Grade STAAR Math Practice Test
- How to Solve Word Problems by Adding Three or More Decimals
- A Comprehensive Collection of Free HSPT Math Practice Tests
- 5 Best Algebra 1 Books for High School Students
- Legislative gridlock
- The Best Grade 8 Math Book for Virginia Students
- FREE 7th Grade OST Math Practice Test
- BMR Calculator


What people say about "Ulam Spiral Explained: Why Primes Form Diagonals - Effortless Math"?
No one replied yet.