The Ulam Prime Spiral, Explained
After reading this you will know why the primes fall along diagonal streaks when you wind the whole numbers into a square spiral, how to name the exact quadratic behind any streak, and what the pattern does and does not tell you about how primes are distributed.
What the spiral is and one streak to fix your eye on
Write the whole numbers in a square spiral. Put 1 in the center, then step right to 2, up to 3, left to 4 and 5, down to 6 and 7, and keep winding outward. Now mark every prime with a bright dot and leave the composites dark. That is the Ulam spiral, named for Stanisław Ulam, who drew it by hand during a dull meeting in 1963.
You expect noise. The primes thin out as numbers grow, so a fair guess is a fog of dots that fades toward the edges. That is not what you see. The dots line up along diagonals. Some diagonals are almost solid runs of primes; others are almost empty. The eye picks out slashes across the grid immediately, and they hold up as you draw more numbers.
Here is the streak to watch. Start a spiral at 41 instead of 1. A long diagonal running from the center is packed with primes: 41, 43, 47, 53, 61, 71, 83, 97, 113, and it keeps going. That diagonal is the polynomial n^2 + n + 41, Euler's famous prime factory, which returns a prime for every n from 0 to 39.
When the spiral is worth drawing, and when it is not
Use the spiral when you want to see that primes carry structure beyond their local density. It is an honest picture of a real fact: some quadratic polynomials produce far more primes than others, and the spiral geometry turns each such polynomial into a straight line, so the human eye can spot them.
Do not use the spiral to measure prime density or to predict the next prime. The streaks are qualitative. Two diagonals that look equally bright can differ by a factor of two in prime yield, and your eye will not tell them apart. For counting, use the prime counting function \pi(x) and the density estimate 1/\ln x, not a picture.
A bright diagonal is not proof that its polynomial gives infinitely many primes. Whether any polynomial like n^2 + 1 yields infinitely many primes is an open problem. The spiral shows a pattern; it does not close the question.
Why diagonals are quadratics
Trace one diagonal of the square spiral and read off the numbers. Along the main southeast diagonal you meet the odd squares: 1, 9, 25, 49, 81. Those are (2n+1)^2 = 4n^2 + 4n + 1. Every diagonal, in every direction, is a run of numbers spaced by a growing even gap, and that always produces a quadratic in n.
Here n counts steps along the diagonal, b and c are integers fixed by where the diagonal sits. The 4n^2 term appears because one full loop of the spiral adds 8n cells, and the corner-to-corner spacing along a diagonal doubles that curvature into a leading coefficient of 4. Different diagonals just shift b and c.
Now the reason some diagonals glow. A quadratic f(n) can never be prime for values of n where a small factor always divides it. If f(n) is even for every n, that diagonal is dead after 2. If it dodges divisibility by 2, 3, 5 and other small primes for many residues of n, far more of its values survive as primes. Euler's n^2 + n + 41 is the champion small case: n^2 + n is always even, so adding the odd 41 keeps it odd, and 41 is chosen so the polynomial also avoids 3, 5, 7, ... for a long stretch.
Reproducing Euler's diagonal from the defaults
Run the tool with its default spiral starting at 1, then compare against the shifted version. The demo draws the plain Ulam spiral; the numbers below are the values sitting on Euler's line, which you can verify by hovering.
- Take f(n) = n^2 + n + 41.
- Compute f(0) = 41, prime.
- f(1) = 1 + 1 + 41 = 43, prime.
- f(2) = 4 + 2 + 41 = 47, prime.
- f(3) = 9 + 3 + 41 = 53, prime.
- Jump to f(10) = 100 + 10 + 41 = 151, prime.
- Now the first failure: f(40) = 1600 + 40 + 41 = 1681 = 41^2, composite. Any polynomial n^2 + n + c must fail at n = c - 1, because f(c-1) is divisible by
c.
So the streak is 40 primes long and then breaks, exactly as the footnote on the tool page states.
Counting how rich a diagonal is
Compare two polynomials head to head over the range n = 0 to n = 999. Euler's n^2 + n + 41 produces 581 primes among its 1000 values. A plainer quadratic like n^2 + 1 produces 418. A deliberately poor one, n^2 + n + 6, produces only about 0 useful primes because n^2 + n is always even and 6 is even, so every value is even and divisible by 2.
The Hardy-Littlewood conjecture F predicts each of these counts from the polynomial's behavior modulo small primes. It is a conjecture, not a theorem, but it matches the observed counts closely: for n^2 + n + 41 up to 1000 it predicts roughly 590, within two percent of the measured 581.
The density of primes near a value x is about 1/\ln x. At x = 10^6 that is about 0.072, so a random line would show roughly 72 primes per 1000 values. A rich quadratic beats that by concentrating survivors, not by breaking the overall thinning of primes.
Try the polynomials yourself
The single idea a still picture cannot show is how the constant term reshapes an entire diagonal. Slide it and watch a bright streak go dark.
Klauber's triangle and the Sacks spiral
The square spiral is only one way to lay out the integers. Two other layouts turn the same quadratics into curves.
- Klauber's triangle
- Write the numbers in rows, row
kholding the numbers from (k-1)^2 + 1 to k^2, centered. Predates Ulam by decades (1932). The quadratic diagonals become gentle vertical curves fanning down the triangle. - Sacks spiral
- Place integer
kat polar angle 2\pi\sqrt{k} and radius \sqrt{k}, so the perfect squares fall on a single ray. Prime-rich quadratics now sweep out smooth curved arms. Euler's polynomial becomes one long clean spiral arm.
All three layouts show the same underlying fact from different angles: quadratic polynomials sort primes into visible families. The layout changes the shape of the streak; it does not change which numbers are prime.
Common mistakes when reading the picture
The pattern invites three errors. First, treating brightness as exact density. Your eye cannot distinguish 581 from 500 primes over a diagonal; use the count, not the glow.
Second, thinking the spiral proves something new about primes. It does not. It visualizes prime-rich quadratics, a phenomenon known and quantified by conjecture F. The picture is a teaching aid, not a result.
Third, expecting the streaks to vanish if you start elsewhere. They do not. Start the spiral at 17 or 1000 and the diagonals reappear, because they come from the polynomials, not from the number 1. That robustness is exactly why the pattern is worth taking seriously, as the tool footnote notes.
Related tools
If the pull between hidden order and apparent randomness interests you, several other toys on this site touch it from different directions. Collatz Orbits chases another simple integer rule with unproven behavior. Conway's Game of Life and Elementary Cellular Automata grow structure from tiny local rules. For continuous fractal structure, Mandelbrot Explorer and the Magnetic Pendulum Fractal show detail at every scale. And Phyllotaxis builds spirals of its own from the golden angle, a different route to arms and rays.
Frequently asked questions
Why do the primes form diagonal lines in the Ulam spiral?
Each diagonal is a quadratic polynomial in the step number, of the form 4n^2 + bn + c. Some of these quadratics avoid small factors for many values of n, so they carry far more primes than average. On the spiral geometry those prime-rich quadratics appear as bright straight lines.
Does the pattern prove anything about prime distribution?
No. It illustrates that prime density varies between quadratic polynomials, which is already described by the Hardy-Littlewood conjecture F. Whether specific quadratics like n^2 + 1 contain infinitely many primes remains unproven.
What is Euler's polynomial and why is it special?
n^2 + n + 41 returns a prime for every n from 0 to 39, a run of 40 primes, and stays unusually rich far beyond that. Over n = 0 to 999 it gives 581 primes, against about 418 for n^2 + 1.
What is different about the Sacks spiral?
It places integer k at radius \sqrt{k} and angle 2\pi\sqrt{k}, which lines up the perfect squares on one ray. The same prime-rich quadratics that make straight diagonals in the square spiral become smooth curved arms in the Sacks layout.
Does starting the spiral at a number other than one change the streaks?
No. The diagonals come from the polynomials, so they persist at every scale and for every starting value. Starting at 41 simply lines up Euler's rich diagonal with the center for a clean view.