The idea: write the numbers in a spiral and mark the primes

In 1963 the mathematician Stanisław Ulam, bored in a meeting, wrote 1 in the middle of a sheet of squared paper, 2 to its right, and kept going round in a square spiral: 3 above 2, then 4, 5 to the left, 6, 7 down, and so on, turning left whenever he could. Then he circled the primes. They were not scattered evenly: many of them fell on diagonal lines. This picture is the Ulam spiral.

Left: the numbers 1 to 49 in a 7 by 7 square spiral, 1 in the middle, with a grey path from 1 to 2 to 3 and onwards; the 15 primes have blue cells. Right: the same grid with the four diagonal rays from 1 labelled: up-right 3, 13, 31 is 4n² − 2n + 1, up-left 5, 17, 37 is 4n² + 1, down-left 7, 21, 43 is 4n² + 2n + 1, down-right 9, 25, 49 is (2n + 1)²
1 … 49 in a spiral, primes in blue. The four diagonals from 1 are the ring corners: each is a quadratic in n.

The canvas starts with 7 × 7. Demo: write 1 … 49 in a spiral, then light the primes writes it one ring side at a time and then lights the 15 primes one by one. Bigger sizes draw each prime as a dot: Demo: zoom out 7 → 21 → 101 → 401.

Rings, corners and quadratics

The spiral is built from rings. Ring k (k = 1, 2, 3, …) is the border of the square of side 2k + 1 around 1. Everything inside it is the square of side 2k − 1, which holds (2k − 1)² numbers, so ring k holds the next 8k numbers, from (2k − 1)² + 1 up to (2k + 1)², 2k on each side. Its four corners are

corner of ring kvaluek = 1, 2, 3, …
up-right(2k − 1)² + 2k = 4k² − 2k + 13, 13, 31, 57, 91
up-left(2k − 1)² + 4k = 4k² + 15, 17, 37, 65, 101
down-left(2k − 1)² + 6k = 4k² + 2k + 17, 21, 43, 73, 111
down-right(2k − 1)² + 8k = (2k + 1)²9, 25, 49, 81, 121

Each corner moves one cell along a diagonal from one ring to the next, so each diagonal ray from 1 is the list of values of a quadratic 4n² + bn + c. Any other diagonal ray works the same way: a cell at a fixed distance d from a corner holds the corner's value plus or minus d, so it is 4n² + bn + c with a different c. The leading 4 is always there because each ring is 8 numbers longer than the one before (Demo: the diagonals near the centre are quadratics).

Half the diagonals are all even

Each step of the path moves one cell and adds 1 to the number, so the number's parity flips exactly when x + y flips: n is odd where x + y is even, like the black squares of a chessboard. A diagonal keeps x + y (or x − y) of one parity, so every diagonal is either all odd or all even. The even ones hold no primes at all (except 2), which is half of why the odd ones look so busy. The page's "typical odd diagonal" is the average share of primes over the odd diagonals that cross at least half of the grid.

Prime-rich and prime-poor quadratics

Pick a line in the line menu and its values are ringed: red if prime, orange if not. The facts panel counts them. At 401 × 401 (the numbers up to 160 801):

linevalues ≤ 160 801primesharevs a typical odd diagonal
n² + n + 41 (Euler)40127167.6 %3.6 ×
4n² − 2n + 4120113567.2 %3.6 ×
4n² + 12015728.4 %1.5 ×
4n² + 2n + 12014321.4 %1.2 ×
a typical odd diagonalabout 20018.6 %1 ×
(2n + 1)²20100 %0 ×

(Demo: 4n² + 1 vs 4n² + 2n + 1 vs (2n + 1)² shows the last three on 201 × 201.) The difference comes from small prime factors. A random odd number is divisible by 3 one time in 3, by 5 one time in 5, and so on; each of those knocks it out. A quadratic can dodge some of them completely:

  • (2n + 1)² is a square, so it is never prime.
  • 4n² + 2n + 1 is divisible by 3 whenever n leaves remainder 2 when divided by 3 (21, 111, 273, …), and by 7 for two n out of 7: it loses more than a typical line.
  • 4n² + 1 = (2n)² + 1. A prime p can divide it only if −1 is a square mod p, which happens exactly when p = 2 or p leaves remainder 1 when divided by 4. So 3, 7, 11, 19, 23, … never divide it.
  • n² + n + 41 has no root mod any prime below 41, so no prime below 41 ever divides it. That is why n = 0, 1, …, 39 all give primes: 41, 43, 47, 53, 61, …, 1601. At n = 40 it is 40² + 40 + 41 = 41², and at n = 41 it is 41 · 43.
Bar chart of the share of primes among the values up to 160 801: n² + n + 41 67.6 percent, 4n² + 1 28.4 percent, 4n² + 2n + 1 21.4 percent, a typical odd diagonal 18.6 percent, all odd numbers 18.3 percent, (2n + 1)² 0 percent; next to each bar the small primes that never divide that polynomial
The fewer small primes can divide a quadratic, the more of its values are prime.

Euler's n² + n + 41 on the spiral

Euler noticed n² + n + 41 in 1772. On the spiral it splits in two: the odd n give 4m² − 2m + 41 and the even n give 4m² + 2m + 41 (put n = 2m − 1 or n = 2m). Near the centre the values wander, because 41 is far from a corner. From ring 20 out they settle on two diagonal rays: 1601, the up-left corner of ring 20, starts the ray along y = x + 40 going up-right, and 1681 = 41², the down-right corner, starts the ray along y = x − 40 going down-left. So the famous 40 primes are the wandering part, and the straight rays that you see on 401 × 401 are its later values, still 64 % prime (Demo: Euler's n² + n + 41, a diagonal of primes). Start the spiral at 41 instead of 1 and the same 40 primes lie on one unbroken diagonal through the centre.

Hardy–Littlewood conjecture F

In 1923 Hardy and Littlewood turned the counting of small factors into a precise guess. For a quadratic an² + bn + c, the number of primes among its first x values should be about

C · x / ln x        C = a product over the primes p of  (1 − w(p)/p) / (1 − 1/p)

where w(p) is how many n in 0 … p − 1 make the polynomial divisible by p. A prime that never divides it (w(p) = 0) makes C bigger; a prime that divides it twice as often as it divides a random number makes C smaller. For n² + n + 41 the constant is about 3.3, which is why its line looks 3 to 4 times as dense as a typical one. Nobody has proved that any quadratic, even n² + 1, takes infinitely many prime values: this is still conjecture F, and the numbers above are only checks against it.

The primes are not on lines

It is tempting to read the picture as "primes like diagonals". They do not: the primes are just the primes. The square spiral happens to put the values of each quadratic 4n² + bn + c on one diagonal, and the quadratics differ in how often they can be prime. Diagonals that are rich in primes are rich because of their polynomial, not because of where they are on the paper. Draw the same numbers in another arrangement and the same polynomials make other shapes.

The Sacks spiral

In 1994 Robert Sacks put each n at polar coordinates (√n, 2π√n): distance √n from the centre, and one full turn between consecutive squares, so 1, 4, 9, 16, … all lie on the ray to the right. The spiral through the points is an Archimedean spiral, and neighbours are always about π apart, so no gaps or corners appear. A quadratic n² + bn + c has a square root close to n + b/2, so its values curve in towards the right-hand ray when b is even and towards the left-hand ray when b is odd. The prime-rich curves of the Sacks spiral are the same polynomials as the Ulam diagonals (Demo: the Sacks spiral, n at angle 2π√n, with (2n + 1)² on the right-hand ray and then Euler's polynomial).

Left: the Sacks spiral for n up to 100, n at radius √n and angle 2π√n, with the squares 1, 4, 9, 16, 25 on the ray to the right and the primes as blue dots. Right: the primes up to 10 000 on the Sacks spiral, forming curved streaks
Sacks spiral: one turn per square, so the squares line up on one ray and quadratics become curves.

The primes themselves come from a sieve: see Sieve of Eratosthenes, and Prime Factorization for what "no small factor" means. Whether −1 is a square mod p is clock arithmetic, as on Modular Exponentiation. Putting the n-th point at radius √n is also how a sunflower packs its seeds: Sunflower Phyllotaxis. Other pictures made of whole numbers: The Times-Table Circle and the Collatz tree.

What the page leaves out

Spirals that start at another number (start at 41 and Euler's primes form one line through the centre), hexagonal and triangular spirals, the exact Hardy–Littlewood constants and how well they match the counts, the prime number theorem behind the 1 / ln n density, sizes beyond 401 × 401 (160 801 numbers), and the many other prime-rich polynomials, such as n² + n + p for Euler's other "lucky numbers" p = 2, 3, 5, 11 and 17.