Leonardo's profile image

Leonardo Errati

Cryptography PhD student @ PoliTo, Italy.

A galaxy of prime numbers

A personal twist on the prime number spiral.

first made 15 April 2023, last modified 9 August 2025

A galaxy of prime numbers

Most mathematicians know that the representation of prime numbers pp in polar coordinates (r,Īø)=(p,p)(r,\theta) = (p,p) results in a cool-looking spiral.

Classical prime spiral

For now, this will not be an in-depth overview.

One day I noticed that it looks quite like a galaxy: a "barred spiral galaxy" to be precise, like our own! Can we add more to it?

Star classes

Astronomers classify stars according to their spectral characteristics. I enjoy astronomy, and I am (partially) confident they know what they are doing.

The following can be found on Wikipedia [1], and is a list of star classes in order of temperature:

ClassChromaticityFreq.
Oblue0.00003%
Bblue-white0.12%
Awhite0.61%
Fyellow-white3.00%
Gyellow7.60%
Klight orange12.00%
Myellow-white76.00%

A cool fact is that they can be memorised with the mnemonic "Our Bright Astronomers Frequently Generate Killer Mnemonics!" and to each class corresponds a different color ("chromaticity"). That is not how we see them from earth, filtered by the atmosphere, but rather how they look from space. In the words of Mitchell Charity [2],

The colors here are for the big balls of hot gas in space, rather than the pin-pricks of light in the local sky.

These pesky astronomers are clearly having fun and we are missing on it.

Prime number classes

My idea was to compare the frequency of star classes to the frequency of (some) prime number classes, and to each associate the respective chromaticity. This is a tricky business: to build an interesting plot we should use meaningful classes, but also keep them close to the star classes frequencies.

Q1: But how many primes should we choose?

I noticed that adding too many prime numbers makes for a weird-looking spiral. Doing some tests, stopping at around 4000040000 returned the closest thing to our galaxy. There are 42034203 primes up to 4000040000, and we must keep it in mind to get matching percentages.

Q2: And which primes should we choose?

Now, we have to find the correct prime number classes. Remember that we are considering all primes up to 4000040000: what is the frequency of each class? For the algebra nerds among us, this table represents all candidate classes and my conclusions. The wisest among you might wish to skip it.

Prime number classFreq.Well-known?
Balanced4.95%āŒ
Circular primes0.79%āŒ
Cousin primes27.62%āŒ
Cullen primes0.000025%āŒ
Emirps (reversible primes)23.32%āŒ
Even primes0.000025%āœ…
Fermat primes0.12%āœ…
Fibonacci primes0.19%āœ…
Happy primes16.35%āŒ
Mersenne primes0.10%āœ…
Palindromic primes1.67%āŒ
Pythagorean primes49.41%āŒ
Primorial primes0.21%āŒ
Safe primes7.76%āœ…
Sophie-Germain primes13.44%āœ…
Strobogrammatic primes0.17%āŒ
Strong primes48.94%āœ…
Twin primes28.10%āœ…

Let us have a look at the lucky winners.

Circular primes are stubborn numbers that keep being prime even if one rotates their digits. An example is 37→7337\rightarrow73, but also 113→131→311113\rightarrow131\rightarrow311.

Even primes are those primes that are also odd. Unsurprisingly, 22 is the only one.

Mersenne primes are those of form 2nāˆ’12^n-1 for some integer nn, they are very well-known for the (original) Mersenne conjecture.

Palindromic primes are primes where their palindrome is also prime. They are not well-known, even though Belphegor's Prime 10000000000000666000000000000011000000000000066600000000000001 has some fame. A fun fact: palyndromic primes whose center is 666666 are called beastly palindromic primes. Perhaps a fitting name.

Safe primes are those primes pp of form p=2q+1p=2q+1, where qq is another prime. They are of strong cryptographic interest.

Sophie-Germain primes are the converse of safe primes, they are primes pp such that q=2p+1q=2p+1 is another prime. Of course, they share the relevance of safe primes.

All comparisons drawn, we get the following result.

Star classFreq.Prime number classFreq.
O0.00003%Even0.000025%
B0.12%Mersenne0.12%
A0.61%Circular0.79%
F3.00%Palind.1.67%
G7.60%Safe7.76%
K12.00%Sophie-Germain13.44%
M76.00%All others76.21...%

Plotting the galaxy

We now have everything we need: our seven prime number classes, their respective colors, and the interval [0,40000][0,40000]. All that remains is to plot them. This just takes a few lines in Python, resulting in the picture below.

Galaxy prime spiral

Keen observers might notice some packs of purple mist in the background. I added as nebulae Carmichael's numbers - which are not prime! The surprising fact about them is that they manage to fool all possible instances of Fermat's primality test in thinking they are prime, while they are not. Being somewhat rare and deceiving a famous primality test, it only seemed fitting to grant them a spot.