A quick post in celebration of Pi Day (March 14th, of course). Here is a piece of sample code for slowly deriving pi using Machin’s approach. I say slow because it takes quite a while to approach pi. Using the Chudnovsky (see Chudnovsky brothers) series, we can approach pi much more rapidly. Speed aside, this works as a fun test to crunch out a few orders of magnitude of pi. One of the projects I had been working on was crunching pi using the Chudnovsky series in parallel, using a split binomial calculation to share the load across several clients. I hope to get this code back on the workbench soon and post the results here. Until then, Happy Pi Day! On to the post.

Journeys into advanced computational derivations of Pi

Programmed in python and output generated by googlechart via pygooglechart api.

This graph shows the occurence of each number in the output of pi calculated to 10**5 as calculated by Machin’s approach.

def arccot(x, unity):
sum = xpower = unity // x
n = 3
sign = -1
while 1:
xpower = xpower // (x*x)
term = xpower // n
if not term:
break
sum += sign * term
sign = -sign
n += 2
return sum
def pi(digits):
unity = 10**(digits + 10)
pi = 4 * (4*arccot(5, unity) - arccot(239, unity))
return pi // 10**10
piString = str(pi(10**5))
valsDic = {}
for x in range(0,10):
i=0
for t in piString:
if int(t) == x:
i+=1
valsDic[x] = i
chart = StackedVerticalBarChart(400,200, y_range=(8000,10500))
chart.add_data(valsDic.values())
left_axis = range(8000,10500,500)
chart.set_axis_labels(Axis.LEFT, left_axis)
chart.set_axis_labels(Axis.BOTTOM, valsDic.keys())
chart.download('pi_10_5.png')