Happy Pi Day!

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.

Machin's Formulation of Pi

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

distribution of 10**5 digits of Pi by Machin's methodology
def arccot(x, unity):
	sum = xpower = unity // x
	n = 3
	sign = -1
	while 1:
        	xpower = xpower // (x*x)
        	term = xpower // n
        	if not term:
        	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):
	for t in piString:
		if int(t) == x:
	valsDic[x] = i

chart = StackedVerticalBarChart(400,200, y_range=(8000,10500))
left_axis = range(8000,10500,500)
chart.set_axis_labels(Axis.LEFT, left_axis)
chart.set_axis_labels(Axis.BOTTOM, valsDic.keys())

this is pi calculated to 10**5 using the Machin approach [very slow] : Click to download [10**5 of pi txt file]

For a good read on all things pi, check out Jorg Arndt’s Pi Unleased.