25 July 2014

Prime number fabric

A prime number is a positive integer only divisible by 1 and by itself. Counting 1 in (usually it's left out) they are 1,2,3,5,7,11,13,17,... It has been known since antiquity that there are an infinite number of them, but their exact distribution is still a mystery.

Stanislaw Ulam devised a simple yet striking way to visualize them. Starting with 1, you arrange the consecutive numbers in a spiral: you move one step to the right, then one down, then one left, one more left, up, up, right, etc. Leaving non-primes white, you end up with a complex pattern called Ulam's Spiral, which nevertheless seem to display several diagonal lines.

Recently, I ran into a remarkable variant of the Ulam Spiral on the Small Satellites website. Each number in the spiral is coloured according to the simple rule: the more divisors, the lighter. Prime numbers, having no proper divisors, are black and the numbers with the highest number of divisors (in the sample considered) are white. The others are between those extreme values in the colour scheme. Below is a sample with the first 25 numbers. Numbers with two proper divisors (4=2*2, 6=2*3, 9=3*3,...) are red, those with three  (8=2*2*2, 12=2*2*3,...) are yellow, with four (16=2*2*2*2, 24=2*2*2*3,...) white. In this sample there are no numbers with more than four proper divisors.


Here is what you get with 40,000 numbers:


 We leave you to admire this mysterious fabric with its intricate filaments. And to say that this glimpse of God's intimate mathematical brain is revealed by these few lines of Mathlab code:

sz  = 201;
mat  = spiral(sz);
matf = zeros(sz);
for i = 1:sz
    for j = 1:sz
         fac = factor(mat(i,j));
         fm = size(fac);
         matf(i,j) = fm(2);
    end
end
figure('Position',[0 0 800 800]);
imagesc(matf);
colormap hot;
caxis([1, max(matf(:))]);axis off;
You don't need to understand Mathlab to run this simple program and so I did. Here is what I got putting sz = 1001. It's a coloured X-ray of the first million integers.


To our human brains (known to be easily fooled, let's not forget it), some delicate structure seems to persist. Not much is left of it in the 'simple' black-and-white Ulam spiral (black=prime). Here it is with a million integers.



Feel free to toy with it yourself. Just copy-paste into Mathlab the code:

clc; clear all; close all;
sz  = 1001;
mat = spiral(sz);
pm  = ~isprime(mat);
figure('Position',[0 0 800 800]);
imagesc(pm);
colormap bone;
caxis([0, 1]);axis off;

P.S. Patterns are not incompatible with chaos. Below is Stephen Wolfram's simple rule 30 automaton in action, 500 steps long. Some regularities can be seen (obvious diagonal bands in the left-hand side), yet the overall behaviour is chaotic in the strictest sense. (See here.)


Wolfram calls this phenomenon —randomness arising from perfectly deterministic simple rules— probably the single most surprising scientific discovery he ever made. (A new kind of science, p. 27).