Video 3, chapter 8

The Ulam Spiral, Seeing the Primes

How the Ulam spiral makes prime numbers line up along diagonals, why each diagonal follows a quadratic polynomial, and why no prime-generating polynomial, not even n squared plus n plus 41, gives primes forever.

  • Video 3, chapter 8
  • When numbers become pictures

Write 11 in the middle of a sheet of squared paper, wind the next whole numbers around it in a square spiral, and mark every prime. The marks gather along diagonal lines, although the primes themselves come at irregular spacings. Where do the streaks come from, and does some line hold only primes? This page builds the spiral, shows that the numbers on each diagonal are the values of a quadratic polynomial, uses divisibility to explain why some diagonals are crowded with primes and others hold none, and shows why a formula that gives forty primes in a row still proves nothing about the next value.

Key terms

Ulam spiral

Definition The Ulam spiral places the positive whole numbers in the squares of a grid, one number per square. The number 11 goes in a center square and 22 in the square to its right. After that the path turns left, a quarter turn counterclockwise, whenever the square on its left is still empty, and otherwise goes straight on. So 33 sits above 22, then 44 and 55 go to the left, 66 and 77 go down and 88 and 99 go to the right, which completes the first ring around 11 at its lower right corner. Then the primes are marked. In a large spiral the marks cluster along diagonal lines, for two reasons. Every diagonal line holds only odd numbers or only even numbers, so half of the diagonals hold no prime except 22. And far enough from the center, the numbers on a diagonal line are the values of a quadratic polynomial, whose divisibility properties make some of the odd diagonals rich in primes and others poor. The picture raises good questions about primes, but it proves nothing about all of them. It is named after the mathematician Stanisław Ulam, who drew it in 1963.

In plain words Coil the counting numbers around a center and color the primes. The coil lines up numbers that follow simple formulas, and some of those formulas are friendlier to primes than others.

Example Here are the first two rings, the numbers 11 to 2525, with the primes in bold.

17161514131854312196121120789102122232425\begin{array}{ccccc} \mathbf{17} & 16 & 15 & 14 & \mathbf{13} \\ 18 & \mathbf{5} & 4 & \mathbf{3} & 12 \\ \mathbf{19} & 6 & 1 & \mathbf{2} & \mathbf{11} \\ 20 & \mathbf{7} & 8 & 9 & 10 \\ 21 & 22 & \mathbf{23} & 24 & 25 \end{array}

Color the grid like a checkerboard with 11 on a dark square. Each step of the path moves to a neighboring square, which has the other color, and changes the number by 11. So the odd numbers all sit on dark squares and the even numbers on light ones. A diagonal line stays on one color, and a line of light squares, such as 16,4,2,1016, 4, 2, 10, holds no prime other than 22. The diagonal from the top left corner to the bottom right corner reads 17,5,1,9,2517, 5, 1, 9, 25, and the other one reads 21,7,1,3,1321, 7, 1, 3, 13, all odd.

Prime numbers

Definition A prime number is a whole number greater than 11 whose only positive divisors are 11 and itself. A whole number greater than 11 that is not prime is composite, which means it is a product a×ba \times b of two whole numbers that are both greater than 11. The number 11 is neither prime nor composite. Every whole number greater than 11 is a prime or a product of primes, in only one way apart from the order of the factors, and there are infinitely many primes, as Euclid proved. To test whether a whole number n>1n > 1 is prime, it is enough to try the primes up to n\sqrt{n} as divisors. If n=a×bn = a \times b with 1<a≤b1 < a \le b, then a≤na \le \sqrt{n}, and every prime factor of aa divides nn as well.

In plain words Primes are the whole numbers above 11 that cannot be split into a product of two smaller whole numbers. Every other whole number above 11 is built from them by multiplication.

Example The primes below 5050 are 2,3,5,7,11,13,17,19,23,29,31,37,41,432, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43 and 4747, fifteen of them, and the gaps between neighbors, 1,2,2,4,2,4,2,4,6,2,6,4,2,41, 2, 2, 4, 2, 4, 2, 4, 6, 2, 6, 4, 2, 4, change irregularly. To test 3737, note that 37≈6.08\sqrt{37} \approx 6.08, so only 22, 33 and 55 need checking. The number 3737 is odd, its digit sum 1010 is not a multiple of 33, and it does not end in 00 or 55, so 3737 is prime. The number 6565 ends in 55, and 65=5×1365 = 5 \times 13 is composite.

Quadratic polynomial

Definition A quadratic polynomial in a variable kk is an expression ak2+bk+cak^2 + bk + c with fixed numbers aa, bb and cc, where a≠0a \ne 0. At k=0,1,2,…k = 0, 1, 2, \ldots the differences between consecutive values grow by the same amount 2a2a at every step, which makes a quadratic easy to recognize in a list of numbers. On the Ulam spiral, the number kk diagonal steps from 11 in each of the four diagonal directions is

  • lower right, (2k+1)2=4k2+4k+1(2k + 1)^2 = 4k^2 + 4k + 1,
  • lower left, 4k2+2k+14k^2 + 2k + 1,
  • upper left, 4k2+14k^2 + 1,
  • upper right, 4k2−2k+14k^2 - 2k + 1,

for every whole number k≥0k \ge 0. More generally, far enough from the center the numbers along any diagonal line, read outward in one direction, are the values of a quadratic polynomial 4k2+bk+c4k^2 + bk + c with whole numbers bb and cc.

In plain words A quadratic polynomial is a formula whose highest power is a square. On the spiral the square comes from the square rings growing around the center, and the rest of the formula says how far around a ring the diagonal sits.

Example The ring at distance kk from the center is the border of a square of side 2k+12k + 1. The numbers 11 to (2k+1)2(2k + 1)^2 fill that square, so the ring ends at its lower right corner with the number (2k+1)2(2k + 1)^2. Walking backward around the ring lowers the number by 11 at each step. Going 2k2k steps back along the bottom reaches the lower left corner, 2k2k more up the left side reach the upper left corner, and 2k2k more along the top reach the upper right corner. So the upper left corner holds

(2k+1)2−4k=4k2+4k+1−4k=4k2+1,\begin{aligned} &(2k + 1)^2 - 4k \\[0.4em] &\quad = 4k^2 + 4k + 1 - 4k \\[0.4em] &\quad = 4k^2 + 1, \end{aligned}

and the other corners follow in the same way. For k=1,2,3,4k = 1, 2, 3, 4 the formulas give these numbers.

DiagonalFirst four numbersFormula
Lower right9,25,49,819, 25, 49, 81(2k+1)2(2k + 1)^2
Lower left7,21,43,737, 21, 43, 734k2+2k+14k^2 + 2k + 1
Upper left5,17,37,655, 17, 37, 654k2+14k^2 + 1
Upper right3,13,31,573, 13, 31, 574k2−2k+14k^2 - 2k + 1

On the upper left diagonal the differences are 1212, 2020 and 2828, growing by 8=2⋅48 = 2 \cdot 4 each time, as a quadratic with a=4a = 4 requires. The lower right diagonal stands out in the picture as a line with no prime at all, because each of its numbers after 11 is the square of an odd number greater than 11 and so has that odd number as a factor.

Prime-generating polynomial

Definition A polynomial with whole number coefficients is called prime-generating on a range of whole number inputs when all of its values on that range are prime. Every such run ends. No polynomial in one variable with whole number coefficients, other than a constant, has a prime value at every whole number input. The reason is divisibility. If f(m)=pf(m) = p is prime, then f(m+tp)−f(m)f(m + tp) - f(m) is a multiple of pp for every whole number tt, because each power (m+tp)j(m + tp)^j differs from mjm^j by a multiple of pp. So f(m+p)f(m + p), f(m+2p)f(m + 2p), f(m+3p)f(m + 3p) and so on are all multiples of pp. A polynomial that is not constant takes the value pp at only finitely many inputs, so one of these multiples is different from pp, and then it is not prime. The best-known example, studied by Leonhard Euler, is n2+n+41n^2 + n + 41. It is prime for each of the forty inputs n=0,1,…,39n = 0, 1, \ldots, 39, while

402+40+41=1681=412.\begin{aligned} 40^2 + 40 + 41 &= 1681 \\[0.4em] &= 41^2. \end{aligned}

A spiral that starts with 4141 in the center instead of 11, and follows the same path, puts the values n2+n+41n^2 + n + 41 for n=0,1,2,…n = 0, 1, 2, \ldots on the diagonal line through the center from lower left to upper right, alternately on its two sides. Its center, the next 2020 squares toward the upper right and the next 1919 toward the lower left all hold primes, and the 2020th square toward the lower left holds 16811681. No quadratic polynomial in one variable is known to take infinitely many prime values, not even n2+1n^2 + 1.

In plain words A formula can give primes many times in a row and still fail at the next input. A long run of examples is evidence, not a proof.

Example The upper left diagonal, 4k2+14k^2 + 1, gives the primes 55, 1717 and 3737 and then 65=5×1365 = 5 \times 13. Divisibility explains where the multiples of 55 fall. When kk leaves remainder 11 or 44 on division by 55, the square k2k^2 leaves remainder 11, so 4k2+14k^2 + 1 leaves remainder 4+1=54 + 1 = 5, which means that it is a multiple of 55. That happens at k=1,4,6,9,11,…k = 1, 4, 6, 9, 11, \ldots, with the values 5,65,145,325,485,…5, 65, 145, 325, 485, \ldots, and only the first of them is 55 itself. In the other direction, 4k2+14k^2 + 1 is always odd, and since k2k^2 leaves remainder 00 or 11 on division by 33, it leaves remainder 11 or 22, so it is never a multiple of 33. Among k=1k = 1 to 100100 this diagonal holds 3333 primes, the upper right diagonal 3232, the lower left 2525 and the lower right none. Whether the upper left diagonal holds infinitely many primes is not known. For n>1n > 1 the number n2+1n^2 + 1 can only be prime when nn is even, and n=2kn = 2k gives 4k2+14k^2 + 1, so the question is the same as whether n2+1n^2 + 1 is prime for infinitely many nn. Edmund Landau listed it in 1912 among four problems about primes, and it is still open.

Check yourself

  1. Use the lower left formula 4k2+2k+14k^2 + 2k + 1 to find the numbers for k=1k = 1 to 55. Check the first two against the grid of the first two rings, and decide which of the five numbers are prime.
Show the answer

The formula gives 7,21,43,737, 21, 43, 73 and 111111. The grid shows 77 just below and to the left of 11, and 2121 in the lower left corner, as predicted. The number 77 is prime and 21=3×721 = 3 \times 7 is not. For 4343, the primes up to 43≈6.56\sqrt{43} \approx 6.56 are 22, 33 and 55, and none of them divides 4343, so it is prime. For 7373, the primes up to 73≈8.54\sqrt{73} \approx 8.54 are 22, 33, 55 and 77, and none of them divides 7373, so it is prime. Finally 111=3×37111 = 3 \times 37. Three of the five numbers are prime.

  1. Write f(n)=n2+n+17f(n) = n^2 + n + 17. It is prime for n=0,1,…,15n = 0, 1, \ldots, 15, and someone concludes that it is always prime. Find the first input where it fails, and explain why it must fail there without testing for primes. Which later inputs are sure to give multiples of 1717?
Show the answer

At n=16n = 16 the value is 162+16+17=289=17216^2 + 16 + 17 = 289 = 17^2. The failure is built in, because n2+n=n(n+1)n^2 + n = n(n + 1), which at n=16n = 16 is 16×1716 \times 17, so the value is 16×17+17=17×1716 \times 17 + 17 = 17 \times 17. More generally f(n)=n(n+1)+17f(n) = n(n + 1) + 17, and since 1717 is prime, f(n)f(n) is a multiple of 1717 exactly when nn or n+1n + 1 is. That happens at n=16,17,33,34,50,51n = 16, 17, 33, 34, 50, 51 and so on, for example f(17)=323=17×19f(17) = 323 = 17 \times 19 and f(34)=1207=17×71f(34) = 1207 = 17 \times 71. All of these values are larger than 1717, so none of them is prime. This matches the argument in the definition, which starts from f(0)=17f(0) = 17 and finds the multiples f(17)f(17), f(34)f(34), f(51)f(51) and so on. Sixteen primes in a row were evidence, but the polynomial could never give primes forever. The polynomial n2+n+41n^2 + n + 41 fails at n=40n = 40 for the same reason, since 40×41+41=41240 \times 41 + 41 = 41^2.

  1. Show that 33 never divides 4k2+14k^2 + 1, and that 33 divides 4k2+2k+14k^2 + 2k + 1 exactly when kk leaves remainder 22 on division by 33. What does this say about the lower left diagonal?
Show the answer

A whole number kk leaves remainder 00, 11 or 22 on division by 33, and then k2k^2 leaves remainder 00, 11 or 11, since 22=42^2 = 4 leaves remainder 11. Because 44 leaves remainder 11, the number 4k2+14k^2 + 1 leaves the same remainder as k2+1k^2 + 1, which is 11 or 22 and never 00. In the same way 4k2+2k+14k^2 + 2k + 1 leaves the same remainder as k2+2k+1=(k+1)2k^2 + 2k + 1 = (k + 1)^2. Since 33 is prime, this square is a multiple of 33 exactly when k+1k + 1 is, that is, when kk leaves remainder 22.

So on the lower left diagonal the squares at k=2,5,8,…k = 2, 5, 8, \ldots hold multiples of 33, such as 2121, 111111 and 273273, and since these are larger than 33 they are composite. One square in three on that diagonal is ruled out by 33 alone, while the upper left diagonal never loses a square to 33. That fits the counts of 2525 and 3333 primes among the first 100100 squares of these two diagonals. The upper right formula 4k2−2k+14k^2 - 2k + 1 leaves the remainder of (k−1)2(k - 1)^2, so that diagonal loses the squares where kk leaves remainder 11, starting with 57=3×1957 = 3 \times 19 at k=4k = 4, after the prime 33 itself at k=1k = 1.