Skip to main content

Prime Spiral with p5.js

The prime spiral, also known as Ulam spiral, is a neat visualization of prime numbers devised by mathematician Stanisław Ulam.

It arranges positive integers in a square spiral, highlighting the ones that are primes:

If you draw a bigger spiral, you’ll notice patterns forming around some horizontal, vertical, and diagonal lines. They correspond to certain polynomials that are known to produce a high density of prime numbers. Interesting, huh?


See also