Ulam Spiral Screensaver
Ulam Spiral is a free, in-browser screensaver that writes the integers in a square spiral out from the centre, one number at a time, lighting the primes. Stanisław Ulam doodled it during a dull talk in 1963 and noticed the primes lining up along diagonals, which they still do and which mathematics has not fully explained. Start the count at 41 and Euler’s famous prime-generating diagonal lights up. Nothing to download, and ESC exits at any time.
How the Ulam Spiral screensaver works
The grid is walked in a square spiral: one step right, one up, two left, two down, three right, and so on, so consecutive integers sit next to each other and each ring holds the next stretch of the number line. A sieve of Eratosthenes marks every prime up to the largest number that fits on screen.
As each number is placed, a prime gets a bright cell and a composite a faint dot. The spiral reveals itself at a steady pace with a glowing head, and when the screen is full it holds, fades, and restarts from a different starting number.
The diagonals appear because every diagonal of the spiral is a quadratic polynomial in the ring number, and some quadratics — Euler’s n² + n + 41 above all — are unusually rich in primes. Starting the count at 41 puts that polynomial on the main diagonal, where it runs prime for forty consecutive cells.
The default palette splits the primes by their remainder modulo 4 — the two classes behave differently in number theory — and the alternative splits by remainder modulo 6, which is also where the vertical and horizontal stripes come from.
A doodle that became a research question
Stanisław Ulam devised the spiral in 1963, and Martin Gardner popularised it in his Mathematical Games column in Scientific American in 1964. It has since become one of the most reproduced images in recreational mathematics. It remains instructive because it asks a question that is simple but not fully answered: why do prime numbers prefer some diagonal lines?
The partial answer comes from algebra. A diagonal in the spiral corresponds to the values of a quadratic polynomial such as Euler’s n² + n + 41, which produces many primes. Hardy and Littlewood proposed in 1923 an asymptotic formula for how often polynomials of the form ax² + bx + c give primes, and that conjecture remains unproven. The pattern is a picture of those polynomials.
The picture also reflects the overall thinning of primes. By the prime number theorem the chance that a number near n is prime is roughly one over the natural logarithm of n, so the dots get sparser away from the centre. It is also not known whether any quadratic polynomial yields infinitely many primes, which is one of the open problems the spiral hints at.
A worked example
At the defaults — Cycle starts, Cell size 6, Speed 4 — the count begins at 1 in the centre and spirals outward with gold and blue cells scattered along clear diagonal streaks. When it restarts from 41, one diagonal through the centre lights solidly for forty cells in a row — Euler’s polynomial — before the first composite breaks it. The caption counts how many primes have appeared so far.
Settings & tips
- Cell size 3 at fullscreen plots over a hundred thousand integers; the diagonals are clearest at this scale.
- Single colour mode is the classic black-and-white image from Ulam’s paper.
- Compare the 1-start and 41-start spirals: the diagonals are the same lines, shifted.
Frequently asked questions
- Why do primes line up on diagonals?
- Each diagonal of the spiral corresponds to a quadratic polynomial, and certain quadratics produce primes far more often than random numbers of the same size would. Why some quadratics are so prime-rich is still not fully understood; the Hardy–Littlewood conjectures predict it but remain unproven.
- What is special about 41?
- Euler noticed in 1772 that n² + n + 41 is prime for every n from 0 to 39. Starting the spiral at 41 places those values along one diagonal, so it lights up solidly through the centre of the picture.
- Will this slow my computer down?
- It is tuned to run smoothly on an ordinary laptop. If it feels heavy, raise the Pixel size or lower the speed in the settings; every control takes effect immediately.
- Is the Ulam Spiral screensaver free?
- Yes. It runs free in your browser with nothing to download or install, and it keeps working offline once the page has loaded.
- How do I exit the Ulam Spiral screensaver?
- Press the ESC key at any time to leave fullscreen and return to the page.
- Does the pattern depend on where the spiral starts?
- Yes. Starting from a different number moves the diagonals, and some start values give much denser lines than others.
- Is this a proof that primes follow a formula?
- No. The lines show that some quadratic formulas give many primes, but the primes as a whole follow no simple formula.
- Why do the primes thin out toward the edge?
- Primes become rarer as numbers grow, at a rate described by the prime number theorem: near n, roughly one number in ln n is prime.