You know what the internet is like. You click on a link on Twitter which takes you to YouTube which suggests another video, which then suggests another and before you know it you've spent far more time than you intended at the computer.

That's the process that landed me on a series of Numberphile videos. I know about Numberphile through another, much longer series of links. I listened to the NosillaCast, which featured Bart Busschots, who co-hosted the International Mac Podcast (no longer active), on which I appeared with Andrew J Clark who recommended The Prompt (since replaced by Connected) which featured Myke Hurley, who started Relay.FM on which he joined CGP Grey for Cortex, where Grey mentioned Hello Internet which features Brady Haran who makes the Numberphile videos!

Cutting to the chase, I watched a whole bunch of Numberphile videos today on all manner of topics including a number which has long held a fascination for me – pi, or π.

Many years ago, when I was in my early twenties, I was boasting to my father that I had memorised a bunch of digits of pi. I forget how many, but I suspect it was something like 15 or so. He promptly grabbed a piece of paper and slowly wrote out 30 decimal places of pi. The first ones matched mine so I had to assume he was correct with the rest. When I quizzed him on how he did it, he wrote out a poem in which the number of letters in each word corresponded with the decimal digits of pi. While trying (and failing) to find said poem online when writing this post, I discovered this technique is referred to a piphilology and specifically, my father relied on a piem.

In an effort to one-up my father, I took the 30 digits he had furnished and set about learning them by rote. I created for myself an extra login step on the computer terminal at work which required me to enter all 30 digits to continue, and I used this several times a day – though I had a much shorter cut-out passphrase for use when the boss was waiting on me!

To this day, I can still recall those digits, plus another 10 I committed to memory many years later. I swear to you that this is typed entirely from memory.

3.1415926535897932384626433832795028841971

In fact, my Dad's poem had a confusing word (I recall it had an apostrophe or similar) and for a long time I remembered the palindromic sub-sequence ...46364... only later discovering it was correctly ...46264...

After watching the pi-related videos today, I had a mind to get myself to a round 50 digits by committing the next 10 digits to memory. Now, while I could simply have looked up these digits online, I began to wonder, as I have before, whether I could use my Mac to calculate the digits.

A related video (not from Numberphile) that I had watched had a link to some software claiming to do exactly that, but on inspection it appeared not to have been updated in a while and was only provided for Windows and Linux. While I could probably have got the Linux one to work (perhaps in a VM), I began a search for a Mac program that could do it.

It turns out, there's a remarkably simple way to calculate digits of pi on any Mac or Linux system without any software beyond what comes as standard. There's a unix command bc which calculates with "arbitrary precision." Give it the right equation and it'll work with extraordinary numbers of digits.

This site I came across gives a remarkably short script to generate digits of pi to an output file. I'm not sure what that a( ) function is (it is intrinsically hard to search for!), but I ran it for 300 digits and it finished instantly. Then I ran it for 10,000 digits and it finished in 100 seconds. Before I went to bed I left it calculating 100,000 digits. It took – no kidding – 11 hours and 1 second.

While the big one was still running I had a file with 10,000 digits of pi – what to do with it? I'd recently been fiddling with shapes in Affinity Designer, trying to come up with some kind of new wallpaper for my 27" iMac. I would create a new wallpaper, which I could then use to help me learn those next 10 digits, and maybe more.

So what, exactly, did I have? I had a text file which contained 10,000 digits of pi arranged in lines of 68 digits, terminated by a backslash and newline. I figured I needed to combine pairs of lines to get the right sort of shape for fitting a lot of digits on a 16:10 screen. I turned to the Atom text editor and its regular expression search and replace.

Find   : (\d{68})\\\n(\d{68})\\
Replace: $1$2

Now I had half as many lines of 136 characters and no superfluous backslashes. I copied and pasted the lot into a text block in Affinity Designer and chose a suitable font – monospaced, of course – which was Menlo. With a suitable font size to allow the digits to be read, but not enormous, I then trimmed to 42 lines to fit the screen with some space top and bottom. That's 5,710 decimal places (plus the "3.").

For a bit of style, I added a black background and ran a subtle grey 'shimmer' gradient from corner to corner. I think it looks pretty snazzy.

5,710 decimal places of pi on my desktop.

But then I decided I wanted something a bit more... funky. One of the Numberphile videos included a number of clever and artsy representations of pi using various visual techniques including the use of colour. What if I coloured all of the 0s one colour, all of the 1s another colour, etc?

I set about choosing the colours. It quickly dawned on me that picking evenly spaced colours along the hue axis of the hue-saturation-luminance picker would be a good choice. I trialled one of each digit and it looked OK. But how to do roughly 571 of each without going completely batty?

I hit upon a relatively simple technique using a combination of Pages and Atom. In Pages I created a new document with a single line of text "0123456789" and I coloured each of the digits appropriately. I then saved the file as rich text.

Opening the rich text file in Atom, it was reasonably easy to see how each colour was applied to each character. At the top there was a definition of all of the colours and then for each character there was a sequence like the following: \cf2 \strokec2 0

The colours were numbered from 2 through 11 in the order I had defined them, so all I needed to do was replace each "0" with "\cf2 \stroke2 0" then each "1" with "\cf3 \strokec3 1" and so on. It struck me that doing a find and replace on each of the digits was going to be problematic considering replacing the 0s would introduce 2s (as part of the colour definition) so I first did a bunch of search and replaces to switch out 0 through 9 with A through J. Then I was able to replace "A" with "\cf2 strokec2 0" and so on.

Having done the two rounds of replacements, I had a huge wodge of text which I then simply copied and pasted into the rich text file in the appropriate place. A quick preview showed it had worked! You might notice an opening extra "0"which is there because the first digit in the original file was prefixed with a bunch of other codes and so I left it there in case their order mattered. I later edited it out.

 
 

That looked pretty ugly on a white background, but when I copied it into the Affinity Designer file it and set it in Menlo against black, it looked... bright! I reduced the opacity to 50% but it still didn't look right. Time to add a shimmer! I used the transparency tool to create a transparency gradient that varied between 100% and 75%. It was looking better, but the random distribution of the digits still gave an overall flat appearance. What was needed was some kind of hero feature.

I quickly hit upon the idea of using the π symbol itself as a feature. Many fonts' glyphs for π are rather dull and square but I eventually settled on the AppleMyunjo font which has a pleasingly stylish one. I added a giant π in 50% grey, set the blend mode to colour dodge so it would brighten up the colours below it, lowered the opacity until it seemed about right (75%), then finally added a moderate gaussian blur to soften the edges.

Ta da!

So there you have it. 5,710 decimal places of pi, as art. I'm really pleased with the final version. You can click on the image above to see it larger, or click below to download the full 2880 x 1620 pixel version I use on my iMac.

pimono.png  picolour.png

Comment