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 zeroth digit of pi is 3 and the fourth digit of pi is 5.)


Pages: 1 2

8 Responses to “The Digits of Pi”

  1. Matt Weaver said

    My solution, written in C:

    This version calculates the requested digit directly, based on the method described in “Computation of the nth decimal digit of pi with low memory”: (and I must admit I also peeked a little at the implementation at

    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.

  2. programmingpraxis said

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

  3. Graham said

    I modified the fixed-point approach for arccot found
    and used it with Wikipedia’s most efficient Machin-Like formula found
    My submission

  4. 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?


  5. It isn’t pretty but it works:


    $| = 1;

    push(@B, 0);

    $counter = 3;
    for($i = 0;$i<=33223;$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);

  6. David said


    def pi_spigot():
        (q, r, t, k, n, l) = (1, 0, 1, 1, 3, 3)
        while True:
            if 4*q+r-t < n*t:
                yield n
                (q, r, t, k, n, l) = (10*q, 10*(r-n*t), t, k, (10*(3*q+r))//t-10*n, l)
                 (q, r, t, k, n, l) = (q*k, (2*q+r)*l, t*l, k+1, (q*(7*k+2)+r*l)//(t*l), l+2)
    def print_pi(n):
        digits = pi_spigot()
        print('pi =', str(next(digits)) + '.', end='')
        while n > 0:
            print(str(next(digits)), end='')
            n -= 1


    >>> print_pi(20)
    pi = 3.14159265358979323846
    >>> print_pi(1000)
    pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989
  7. David said

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

    >>> print_pi(1000)
    pi = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: