Pi Day Countdown (D-2)

Today, we take a quick look at two methods for computing pi using your humble PC. These two methods are infinite sums, meaning that the value of the sum approaches pi as you increase the number of terms added. Here they are. 

1. Ramanujan’s Formula

image
2. Plouffe’s spigot formula
image
To save you the calculator work, you can use the following python code to execute both calculations. I have listed the results for 10 iterations of each formula below.

from __future__ import division
import numpy as np
import random
import math

np.set_printoptions(precision=15)

def pi_plouffe(n):
    return np.cumsum([x for x in[(4./(8.*k+1.) -2./(8.*k+4.) - 1./(8.*k+5.) - 1./(8.*k+6.))/16.**k  for k in range(n)] ])

def pi_ramanujan(n):
    return (9801/(2*math.sqrt(2)))/np.cumsum([x for x in[math.factorial(4*k)*(1103+(26390*k))/((math.factorial(k)**4)*(396**(4*k))) for k in range(n)]])

print pi_plouffe(10)
print pi_ramanujan(10)
Ramanujan’s formula converges rapidly to 15 digits of pi within 3 iterations!

3.141592730013306,
3.141592653589794,
3.141592653589793,
3.141592653589793,
3.141592653589793,
3.141592653589793,
3.141592653589793,
3.141592653589793,
3.141592653589793,
3.141592653589793,
...
Plouffe’s formula converges less rapidly although it is still fast compared to other infinite series methods. There is also a hidden advantage. The (1/16)^i in Plouffe’s formula means that you can calculate any individual hexadecimal digit of pi independently of the preceding ones. Since the digits flow from the formula like a tap, this is called a spigot formula. This class of formula is a huge advantage for computing long numbers of digits in a memory efficient manner. In 1996, one version of Plouffe’s formula was used to calculate the 10^11th hexadecimal digit of pi.

3.133333333333333,
3.141422466422466,
3.141587390346582,
3.141592457567436,
3.141592645460336,
3.141592653228088,
3.141592653572881,
3.141592653588973,
3.141592653589752,
3.141592653589791,
...

To complete the story, Plouffe’s formula was later improved on by Bellard in 1997 and this was used in the PiHex project to calculate binary digits of pi. Here’s Bellard’s version in case you are curious.

image
Share on:

Comments are closed.