## The Digits of Pi

### February 20, 2009

The ratio of the circumference of a circle to its diameter is given by the constant known by the Greek letter pi, and is an irrational number (its representation is non-terminating and non-repeating) with a value slightly larger than 3.14159. What is the one-thousandth digit of pi? (Counting begins at zero, so the zero^{th} digit of pi is 3 and the fourth digit of pi is 5.)

Advertisements

My solution, written in C:

http://codepad.org/WOEQnbTt

This version calculates the requested digit directly, based on the method described in “Computation of the nth decimal digit of pi with low memory”: http://numbers.computation.free.fr/Constants/Algorithms/nthdecimaldigit.pdf (and I must admit I also peeked a little at the implementation at http://numbers.computation.free.fr/Constants/Algorithms/nthdigit.html)

It lacks the conciseness and elegance of the spigot solution, but makes up for it in efficiency. The spigot version works well until it runs out of main memory and starts paging, at which point performance really hits a wall. On my computer that point is somewhere between the 20,000th digit (took 1:48) and the 50,000th (I killed it after 3.5 hours). For comparison, this solution returns the 50,000th digit in ~12 seconds.

Alexander J. Yee & Shigeru Kondo recently computed five trillion digits of pi.

I modified the fixed-point approach for arccot found

here

and used it with Wikipedia’s most efficient Machin-Like formula found

here

My submission

This seems like a really interesting exercise, however I cannot understand how this spigot algorithm works. I read the PDF, but I could not follow the maths in the diagram, is there an easier explanation for someone who doesn’t have a massive background in maths?

Cheers,

-Sam

It isn’t pretty but it works:

#!/usr/bin/perl

$| = 1;

push(@B, 0);

$counter = 3;

for($i = 0;$i<=33223;$i++){

push(@A,$i);

push(@remainder, 2);

push(@B, $counter);

$counter += 2;

}

for($x = 1; $x=1;$i--){

$m10[$i] = $remainder[$i] * 10;

$sum[$i] = $m10[$i] + $carried[$i];

$remainder[$i] = $sum[$i] % $B[$i];

$carried[$i - 1] = (($sum[$i] - $remainder[$i]) / $B[$i]) * $A[$i];

}

$m10[0] = $remainder[0] * 10;

$sum[0] = $m10[0] + $carried[0];

$remainder[0] = $sum[0]%10;

$predigit = int($sum[0]/10);

push(@predigits, $predigit);

if($predigit < 9){

for($i = 0; $i<((scalar @predigits)-1); $i++){

print $predigits[$i];

}

undef @predigits;

push(@predigits, $predigit);

}

`if($predigit == 10){`

for($i = 0; $i<((scalar @predigits)-1); $i++){

if($predigits[$i] == 9) { print 0; }

else {

$predigits[$i] = $predigits[$i] + 1;

print $predigits[$i];

}

}

undef @predigits;

push(@predigits, 0);

}

}

Python:

Running:

For the curious, all first 1000 digits (it ran off last post there…)

https://github.com/ftt/programming-praxis/blob/master/20090220-the-digits-of-pi/pi.py