The Lancing Prime

Timothy C from the Lower Sixth has achieved an exceptional piece of work, where the Maths and programming required are well beyond A Level preparation. The project 'Lancing Prime' started when Mr Bullen, Head of Mathematics, emailed Timothy a link to a blog post about the ‘Corpus Christie Prime’, created by a student at Cambridge, and suggested they worked together to create a similar Lancing Prime.

Within a couple of days, and without any support from the teaching staff, Timothy had created the image below:

Lancing Chapel Prime Number

Timothy writes: 

"The Corpus Christie Prime is 2688 digits long and constructed from pixel art; our prime is 8946 digits long and constructed from a photograph. With each digit, the size of the number is increased tenfold and with it the computational complexity, but the detail of the image is hugely improved. Yet as amazing as it is, the Lancing Prime is not the only prime which I have found, and with the software I have developed, it is as simple as running a program and waiting. 

Before I explain how this was done, it is important to understand the significance of a number being prime. The definition of a prime is a number that cannot be divided wholly by any integer other than itself and 1. Any natural number can be expressed as a product of primes, but a prime can only be expressed as itself. Prime numbers are the building blocks of all numbers – they cannot be pulled apart into component parts because they are the components.

The definition sounds simple enough, but finding out if a number is prime is deceptively difficult. The naive approach is to attempt to divide by every integer from 2 to the prime, but for a prime with upwards of 5000 digits that algorithm would take longer than the age of the universe to complete. Instead, we must turn to computational number theory, and in particular the Miller-Rabin test, a probabilistic algorithm that tests if a number is prime to a high degree of accuracy - and fast.

Of course, testing if a given number is prime is only half the battle. How do we find numbers that look like images in the first place? Well, different digits need different numbers of pixels to draw, so some will appear darker than others. This means we have 10 different shades to work with when creating art in a number. So I wrote a small python script to convert any grayscale image into a ‘perfect number’ by analysing the pixels row by row and assigning each one to the closest match of the possible digits, in terms of the darkness of the pixel.

Once we have this number, we need to randomly change digits until the number is prime. At this point, we must take care to ensure the program is efficient. It would be easy to change numbers in terms of the way they are represented, as a human would, but this would require computationally intensive conversion operations – within the computer a number is stored in binary, not decimal. So instead we perform digit changes mathematically as follows, where n and d represent the new digit and the position of the digit to change, respectively:


The main program will change digits randomly using this function and feed each resulting number into the Rabin-Miller test, increasing the number of digits changed after every 100 numbers tested. In the case of the Lancing Prime, only 6 digits were changed, but for others it was over 100. When a number passes the Rabin-Miller test, it is formatted back into rows like the original image and tested further with more iterations of the Rabin-Miller test. Then we simply open this file in a word processor, set it to the desired font, and we have a prime number that looks like our original image.

That was a lot of maths and I think it’s easy for us to lose sight of why any of this matters. It’s difficult to disagree with the idea that the Lancing College Chapel is beautiful – but if in a few simple mathematical functions we have discovered that in the building blocks of numbers, in mathematics itself, we can find things as beautiful as anything we find around ourselves – then what does that say about the beauty of maths? The wonders of mathematics often remain hidden from those who don’t look, and what I have made is barely a fragment of these wonders, but I hope this endeavour has begun to express the beauty that a mathematician can see in maths."

Well done Timothy for this outstanding achievement!