-=vyruss=- / blog

  • articles
  • talks
  • tweets
  • blog roll
  • main page

The Angelakos Prime Spiral: A New Way to Visualize Prime Numbers?

  • Dec 19, 2025
  • 5 min read
  • Categories: articles
  • Tags: #maths #math #visualization #visualisation #developer #python

I... seem to have discovered a new prime number visualization (?)

I have been fascinated with doodling square spirals since childhood. There is something satisfying about drawing a straight line, turning 90 degrees, drawing a slightly longer line, and repeating until you fill the page. It's somehow geometrically comforting.

Recently, while watching an unrelated maths video on YouTube, I started wondering about applying a method to my doodling: what if I started drawing a square spiral, but the line lengths were a sequence of prime numbers?

Disclaimer: IANAM (I am not a mathematician). I'm just a bloke who loves a good pattern (and enjoys Python). So if I'm about to describe something painfully obvious to the mathematical community, well, at least I had fun discovering it!

The method

This is very analogous to the Logo programming of turtle walks that I would have seen at school — if only we had computers at school growing up in Greece in the 1980s. (Thanks dad for buying me a Sinclair ZX Spectrum when I was 4!)

  1. Start at the origin (0,0).
  2. Face East (E)
  3. Move forward by a length equal to the next prime number (2, then 3, 5, etc.)
  4. Turn 90° right (now you are facing South (S))
  5. Repeat from 3.

This creates a cycle of directions (E → S → W → N), with each leg of the spiral growing according to the prime number sequence 2, 3, 5, 7, 11, 13, ...

I intuitively expected to see chaos. Given the famous unpredictability of prime gaps, I was ready for a weird mess of a visualization.

The hidden pyramid in the primes

What I actually got was this shape:

The Angelakos Prime Spiral with 1000 primes

The pattern struck me right away: it's not really chaotic and it looks like a pyramid viewed from above. The four corners appear as nearly perfect straight lines radiating from the center.

After staring at it and doing some googling, it turns out that why this "works" is because it's visual proof of the prime numbers' sequence being monotonic. As p(n+1) is always greater than p(n), the spiral can never cross itself:

  • The S leg (p₂ = 3) is longer than the E leg (p₁ = 2)
  • The W leg (p₃ = 5) is longer than the S leg, so it passes the starting zero x coordinate
  • The N leg (p₄ = 7) is longer than the W leg, so it clears the corner, and so on

Every turn makes the sort of "bounding box" a bit larger. Apparently, in maths this is called an infinite spirolateral, but this one specifically will never repeat or self-intersect.

Why are the corners straight(ish)?

The emerging "lines", where the corners of the visual pyramid are, don't look perfectly straight because there is a slight wobble. This wobble represents the data being visualized, the Prime Gaps. When the gap is small, there is a tight corner, and when it's a big gap, the wall bulges out more.

However, as the numbers grow larger, the magnitude of the primes (or "signal", if you want) starts to drown out the variance of the gaps (or "noise", in this case). Visually, this means that the corners appear to get sharper and straighter the further out we go.

Geometric visualization of the prime number theorem?

The prime number theorem (PNT) effectively says that the average gap between consecutive prime numbers tends asymptotically to follow the natural logarithmic sequence ln(pₙ) (shout out to Edinburgh's own John Napier for that one).

So while the gaps between primes are random, their statistical distribution tends to follow a precise and predictable order. This is why the "air" between the walls isn't random and the visualization doesn't become a solid block of black line. The spacing between the spiral arms grows logarithmically proportional to the length of the ever-growing walls, and this apparent separation must be visualizing the decreasing density of prime numbers.

What's still surprising to me is the impressive regularity of primes in aggregate.

Sample code

Here is some Python code which uses my beloved matplotlib. You can run it to generate the spiral yourself:

import matplotlib.pyplot as plt

n = 1000

def draw_spiral(n):
    # Generate first n primes
    primes = []
    i = 2
    while len(primes) < n:
        if all(i % j != 0 for j in range(2, int(i ** 0.5) + 1)):
            primes.append(i)
        i += 1

    x, y = [0], [0]
    pos_x, pos_y = 0, 0

    # Compass factors (E, S, W, N)
    directions = [(1, 0), (0, -1), (-1, 0), (0, 1)]

    for i, prime in enumerate(primes):
        dir_x, dir_y = directions[i % 4]
        pos_x += dir_x * prime
        pos_y += dir_y * prime
        x.append(pos_x)
        y.append(pos_y)

    # Calculate the bounds
    min_x, max_x = min(x), max(x)
    min_y, max_y = min(y), max(y)
    width = int(max_x - min_x)
    height = int(max_y - min_y)

    # Unit = 1 pixel
    dpi = 1
    figsize = (width, height)

    fig, ax = plt.subplots(figsize=figsize, dpi=dpi)
    ax.plot(x, y, linewidth=1, color='black', antialiased=False)
    ax.set_xlim(min_x, max_x)
    ax.set_ylim(min_y, max_y)
    ax.axis('off')
    plt.subplots_adjust(left=0, right=1, top=1, bottom=0)

draw_spiral(n)
plt.savefig(f"spiral_{n}.png", dpi=1, bbox_inches='tight')

Is this new?

This appears to be genuinely novel. Existing prime visualizations seem to fall into two categories:

  1. Position-based (like the Ulam Spiral, 1963, and Sacks Spiral, 1994): These place consecutive integers in a pattern and then highlight where primes occur. The primes determine the markers, not the geometry.
  2. Direction-based (like Gruenberger's Prime Path, 1976, Fraile's Prime Walk, 2021, and the Numberscope turtle visualizer (Stange et al.)): These use properties of primes (like last digits or remainders) to determine the turn direction, while keeping the step length constant at one unit.

This spiral probably deserves a descriptive name like the Prime-Step Spiral to distinguish it from other visualizations.

Until someone inevitably reaches out to point out that it's been done before, and is documented in some obscure 1960s maths journal, I'm calling it the Angelakos Prime Spiral 😊

Announcing the inaugural PostgreSQL Edinburgh meetup
Rendering: Pelican  •  Theme: Peli-Kiera (modified)  •  Copyright © 2025   Jimmy Angelakos