A spiral of first 10k prime numbers…

A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers.

Euclid proved the infinity of primes by contradiction.

  • Assume there are a finite number, n , of primes , the largest being \(p_n\);
  • Consider the number that is the product of these, plus one: \(N = \prod\limits_{i = 1}^n {{p_i}} + 1\)
  • By construction, \(N\) is not divisible by any of the \(p_i\).
  • Hence it is either prime itself, or divisible by another prime greater than \(p_n\), contradicting the assumption.

Euclid’s proof is the easiest to understand, especially if you’re not well-versed in mathematics, but now I am going to introduce another proof of the infinitude of primes, which is shorter and I also find it to be the most appealing, at least in my eyes.

In mathematics, the prime-counting function is the function counting the number of prime numbers less than or equal to some real number \(x\). E.g. for \(x=10.124\), we have that 4 number of prime number: 2, 3, 5, 7.

Let \(\pi(x)\) be a prime-counting function.

The Prime Number Theorem suggests that:

\(\mathop {\lim }\limits_{x \to \infty } \frac{{\pi \left( x \right)}}{{x/\ln (x)}} = 1\)

In other words, we can say that \(\pi(x)\) “behaves” like \(x/\ln (x)\) when \(x\) is approaching infinity — there’s an asymptotic ‘equality’.

So, there are infinite primes.


For Python code, we use SymPy – a Python library for symbolic mathematics. In particular, we’ll use primerange(a, b) for generate all prime numbers in the range [a, b).

The code:

import sympy
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as ani
#plt.style.use('dark_background')
plt.style.use('default')

def convert_to_polar_coordinates(num):
    return num*np.cos(num), num*np.sin(num)


lim = 10000

fig, ax = plt.subplots(figsize = (8, 8))
primes_numbers = sympy.primerange(0, lim)
primes_numbers = np.array(list(primes_numbers))
x, y = convert_to_polar_coordinates(primes_numbers)


def init():
    ax.cla()
    ax.plot([], [])


def plot_in_polar(i):
    ax.cla()
    ax.plot(x[:i], y[:i], linestyle='', marker='o', markersize=0.75, c='#FC0000')
    ax.set_xlim(-lim, lim)
    ax.set_ylim(-lim, lim)
    ax.axis("off")

plt.savefig('prime.png')

animator = ani.FuncAnimation(fig=fig, func=plot_in_polar, interval=1, frames=len(primes_numbers))

animator.save("prime.gif")

plt.show()
A spiral of first 10k prime numbers in a polar coordinate system.