The Klauber Triangle


enter image description here

The Klauber Triangle (first proposed by the American herpetologist Laurence Monroe Klauber) is a simple way to visualize the prime numbers, similar to the Ulam Spiral. It is constructed by creating a triangle of the natural numbers and then highlighting only the primes:

       1                   □
     2 3 4        →      ■ ■ □
   5 6 7 8 9           ■ □ ■ □ □
 ...                 ...

Here is a short Python script to generate the Klauber Triangle above.

This code is also available on my github page.

import numpy as np
import matplotlib.pyplot as plt
import as cm

n = 200
ncols = 2*n+1
nmax = n**2

# Prime numbers up to and including n**2.
primes = np.array([n for n in range(2,n**2+1) if all(
                        (n % m) != 0 for m in range(2,int(np.sqrt(n))+1))])
a = np.zeros(nmax)

arr = np.zeros((n, ncols))
for i in range(n):
    arr[i,(n-i):(n+i+1)] = a[i**2:i**2+2*i+1]

fig, ax = plt.subplots()
ax.matshow(arr, cmap=cm.binary)
# Ensure the Axes are centred in the figure
Current rating: 4.4


Comments are pre-moderated. Please be patient and your comment will appear soon.

There are currently no comments

New Comment


required (not published)