On this page
Make a guess first. Write 1 in the middle of squared paper, spiral outwards anticlockwise, and shade every prime. Will you get random speckle or a pattern? Many people guess speckle, because primes have no simple formula. The picture says otherwise.
Do prime numbers look random on a spiral?
Not quite. Stanislaw Ulam drew this spiral in 1963 while doodling during a dull talk at a scientific meeting, according to Wolfram MathWorld's entry on the prime spiral. When he circled the primes, many fell on diagonal lines.
Ulam, M. L. Stein and M. B. Wells described it in a May 1964 paper, A Visual Display of Some Properties of the Distribution of Primes in The American Mathematical Monthly, noting a "strongly nonrandom appearance" (as quoted by MathWorld).
Ulam first plotted a few hundred points by hand; the MANIAC II computer then extended the plot to about 100,000 points, and the streaks stayed (Wikipedia: Ulam spiral).
How do you draw an Ulam spiral?
Write 1 in a square near the centre of squared paper.
Write 2 to its right and 3 above it, then turn left for 4 and 5, down for 6 and 7, and keep turning anticlockwise.
Stop at 21 cells wide: 1 to 441, with 441 = 21² in the bottom-right corner.
Write your prediction: dots, or lines?
Shade the primes, listing them with the Sieve of Eratosthenes, done interactively.
The diagram at the top is this 21 by 21 spiral. Of its 441 numbers, 85 are prime; a sieve lets you check the count.
Why do primes line up on diagonals?
First, parity: all primes except 2 are odd, so they sit on alternate diagonals, giving a checkerboard feel (Wikipedia: Ulam spiral).
Second, and more important, every diagonal, horizontal or vertical line in the spiral lists the values of a quadratic 4n² + bn + c, where b and c are integers (same source). Some quadratics can never be divisible by small primes, so they hit primes more often than ordinary odd numbers of the same size.
Worked example: the four diagonals that start at 1
Up-right: 3, 13, 31, 57, 91, 133, 183, 241, 307, 381. This is 4n² − 2n + 1, and 5 of these 10 numbers are prime.
Up-left: 5, 17, 37, 65, 101, 145, 197, 257, 325, 401. This is 4n² + 1, and 7 of these 10 are prime.
Down-left: 7, 21, 43, 73, 111, 157, 211, 273, 343, 421. This is 4n² + 2n + 1, and 6 of these 10 are prime.
Down-right: 9, 25, 49, 81 and so on up to 441. This is (2n + 1)², a perfect square every time, so none is prime.
Why does 4n² + 1 do so well? A square n² leaves remainder 0 or 1 when divided by 3, so 4n² + 1 leaves 1 or 2: never a multiple of 3. Squares leave 0, 1, 2 or 4 when divided by 7, so 4n² + 1 leaves 1, 5, 2 or 3: never a multiple of 7. Its composite values need a factor like 5 or 13; 65, 145 and 325 are all multiples of 5.
Its primes are exactly the primes of the form m² + 1, apart from 2. Whether there are infinitely many such primes is one of Landau's problems, all four still unresolved.
Euler's polynomial n² + n + 41 does even better: it gives primes for all 40 values n = 0 to 39 (MathWorld: prime-generating polynomials). Start the spiral at 41 and one diagonal carries an unbroken run of 40 primes (Wikipedia: Ulam spiral).
Do the diagonals mean primes follow a formula?
No. The streaks show that some quadratics are richer in primes than others, not that any formula gives only primes. G. H. Hardy and J. E. Littlewood's 1923 paper Partitio numerorum III contains Conjecture F, which predicts how densely primes fall along such lines; it remains a conjecture.
Primes also thin out. The prime number theorem says the count of primes up to n is roughly n divided by ln n (MathWorld: prime number theorem), so bigger spirals have sparser dots. They never run out, as Euclid's proof that there are infinitely many primes shows.
A 20-minute spiral experiment
Five minutes: draw the 21 by 21 grid and write your prediction.
Ten minutes: number it, sieve the primes up to 441, and shade them.
Five minutes: test the darkest diagonal with second differences, then compare it with the probability that a number from 1 to 100 is prime.
Some ideas are easier to see than to read, which is why we built the Fermi Simulation Lab of interactive simulations. If the remainder argument felt slippery, Fermi's AI tutor works through problems alongside you and checks understanding as it goes.
Now look at your grid: was your first guess right?



