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.

1716151413
1854312
1961211
2078910
2122232425

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:

RayNumbersFormulaPrimes?
Northeast3, 13, 31, 57, 91, …\(4r^2 – 2r + 1\)Many
Northwest5, 17, 37, 65, 101, …\(4r^2 + 1\)Many
Southwest7, 21, 43, 73, 111, …\(4r^2 + 2r + 1\)Many
Southeast9, 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.

Prime Spirals – Numberphile (Numberphile). The Ulam spiral and why its diagonals appear.
Why do prime numbers make these spirals? | Dirichlet’s theorem and pi approximations (3Blue1Brown). A related polar spiral and the real reasons behind its patterns.

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.

1. Who discovered the Ulam spiral in 1963?
Found a bug? Let us know!
2. What kind of formula describes a diagonal of the spiral?
Found a bug? Let us know!
3. For which n does n² + n + 41 first fail to be prime?
Found a bug? Let us know!
4. Is there a polynomial that produces only primes?
Found a bug? Let us know!
5. Which unsolved question asks whether n² + 1 is prime infinitely often?
Found a bug? Let us know!

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

Sources and Further Reading

Related to This Article

What people say about "Ulam Spiral Explained: Why Primes Form Diagonals - Effortless Math"?

No one replied yet.

Leave a Reply