DEV Community

Charles Engelke
Charles Engelke

Posted on • Originally published at engelke.dev on

Runs of Digits in Pi

My colleague, Emma Haruka Iwao, just published the decimal value of Pi to 31,415,926,535,897 places. It’s an amazing accomplishment for all sorts of reasons, not least being how to run a calculation across many machines over several months successfully. Google Compute Engine’s Live Migration capability was key in keeping it running without a break.

But now, how about a much less amazing accomplishment? I wrote a program to scan those digits to look for runs of the same digit. I got the digits from Emma’s results. She has set up a web service anyone can use to fetch a bunch of the digits on demand. Technical details after a few results below.

How far do you have to go in the decimal value of Pi to find two identical successive digits? It turns out to be only a couple of dozen places. ’33’ is found at positions 25 and 26 (counting the first ‘3’ in Pi as position 0, the decimal point as position 1, and so on). Want to find three identical digits in a row? There’s a run of ‘111’ starting at position 34.

Interestingly (at least to me), the first run of more than three digits has six identical digits in a row: ‘999999’ starting at position 763. Want a run of seven? You’ll have to go to position 710,101 to find ‘3333333’. How about a run of eight? I don’t have the answer. I stopped my program from scanning the digits after about 10,000,000 places without finding that long a run.

I wrote a simple Python program to ask the web service for a thousand digits at a time (the most it will serve for each request), and scanned for long runs of digits. I say it’s simple, but it took me at least a dozen tweaks to get it working right. Still, the only special feature of it is how it gets its data: it makes an HTTP GET request to https://api.pi.delivery/v1/pi?start=0&numberOfDigits=1000 (with the 0 and 1000 replaced by the actual starting point and number of digits you want). The response is a JSON object with one field called content. The value of that field is a string containing the digits.

It’s a nice little programming exercise. Try it yourself, and see if you can make as many errors as I did before you get it working.

Top comments (0)