Skip to content
All apps

Prime spiral

About this app

Ulam spiral with up to a million numbers: hover a cell and its diagonal becomes the formula 4t² + bt + c — with start number 41 Euler's polynomial yields 40 primes in a row. The polar view shows spokes turning into spiral arms.

The idea

Write the numbers 1, 2, 3, … in a square spiral around a centre and mark only the primes, and striking diagonal lines appear. This is the Ulam spiral. The reason is algebraic: the numbers along a diagonal are the values of a quadratic polynomial 4t² + bt + c, and some of these polynomials produce primes far more often than average. A well-known example is Euler’s polynomial n² + n + 41, which gives primes for every n from 0 to 39. On a large scale primes still thin out: their share among the first x numbers falls roughly like 1 / ln x (the prime number theorem).

What you see

The app draws the Ulam spiral for up to one million numbers, optionally with composites as faint dots. Hover or tap a cell and the diagonal through it lights up; the panel shows its polynomial, the share of primes on it, the longest unbroken run and what a random distribution would give. A second view shows the polar spiral with r = n and φ = n. The statistics panel compares the exact prime count π(N) with the estimate N / ln N.

Things to try

Set the centre number to 41: Euler’s polynomial then lies on the main diagonal and you see 40 primes in a row. In the polynomial panel you can enter your own coefficients a, b, c or load Euler 41 and Legendre 17; their values are marked as rings on the spiral. Increase N step by step and watch the polar view, where straight spokes turn into spiral arms.

Frequently asked questions

What is the Ulam spiral?
The Ulam spiral arranges the natural numbers in a square spiral and highlights only the primes. Diagonal lines with an unusually high density of primes become visible. It is named after the mathematician Stanisław Ulam, who came across the pattern in 1963.
Why does the Ulam spiral show diagonal lines of primes?
Every diagonal of the spiral consists of the values of a quadratic polynomial 4t² + bt + c. Some of these polynomials never take values divisible by small primes such as 2, 3 or 5, so they hit primes more often than average. The lines are a property of those polynomials, not an artefact of the drawing.
Which polynomial produces the most primes in a row?
The classic example is Euler’s polynomial n² + n + 41, which gives forty primes for n = 0, 1, …, 39. At n = 40 it yields 1681 = 41², so the run ends there. No non-constant polynomial with integer coefficients produces only primes.
What can I do in this app?
You can move across the spiral to see which polynomial belongs to a diagonal and how many primes lie on it. The centre number, the range of up to one million numbers and the view (Ulam, polar or both) are adjustable. In the polynomial panel you test your own quadratic polynomials and mark their values on the spiral.

Subject: Number theory

More from Number theory

A VisuApp by heyprof: interactive, free in your browser, no sign-up. What is a VisuApp? · All apps