Clone wiki

ulam / Home

The Ulam Spiral

The Ulam Spiral is an intriguing pattern found in the natural numbers. Numbers are plotted in a square spiral, starting with 1 at the origin. If we assign colors and sizes to each point in an appropriate way, we can generate some interesting pictures. This repository contains a very simple Python program to generate images of this type. The only dependency is the Matplotlib graphics library.

Below we plot values of n from 1 to 100,000. The size of each point is proportional to the number of divisors. Dots are colored according to the prime number theorem, which states (roughly) that the total number of primes less than n is about n/log(n).

./ -n 100000 -d 100 --theorem -a 0.5 --bgcolor=#000000

Now we plot only prime numbers. The diagonal patterns are apparent:

./ -n 100000 -d 100 -p

Euler found a polynomial n^2 - n + 41 that generates an unusually large number of primes: it is prime for all non-negative integers less than 41. The image above highlights the points generated by this polynomial. Hardy and Littlewood formulated a conjecture regarding quadratic polynomials that generate a large number of primes; the spiral's Wikipedia page has more details.

./ -n 50000 -d 100 -e n**2-n+41

In 2004, Green and Tao proved a remarkable theorem stating that the sequence of primes contains arbitrarily long arithmetic progressions. For example, 3, 5, and 7 form an arithmetic progression of length 3. The prime number theorem implies that primes become "harder to find" as numbers increase, yet the Green-Tao theorem says that certain subsequences are spaced in regular ways. In a sense, this theorem is a weaker form of the twin prime conjecture, which states that there are infinitely many pairs (p, p+2), such that both are prime. (A twin prime is just an arithmetic progression of length 2 with step size 2.) Above we plot the sequence 5 + 6 * n; only the first five numbers of this sequence are prime.


While these theorems are deep and significant, there are many other patterns in the spiral. It is a visual representation of just how little we know about the structure of the primes.