Dick measuring contests of yore were computationally intensive, now that I recall on this Pi Day. Growing up as a snotty, disrespectful middle-schooler with attitude was no different then, except that in 1980s India, I had no TV, no Refrigerator, no Computer and certainly no Internet. My weapons of choice were primarily math. I stole my dad's Calculus text and memorized several esoteric equations which were certainly not age-appropriate, but there was no one to stop me. I deployed these equations much like missiles, to enlarge my domain of control and crush my enemies i.e. classmates who were disadvantaged by their inability to handle said equations.

My favorite equation was the Gregory's Series, a Taylor expansion for the arctangent.

My favorite equation was the Gregory's Series, a Taylor expansion for the arctangent.

Sub in 1 for the x, you instantly get the well-known Leibniz Formula:

Somewhere in the mid-80s, the tide began to turn.

My poor dad saved up enough to buy a Black and White Television.

Soon, his office loaned him an 80286

This was huge! I got a physical copy of QBASIC LRM and quickly figured out that GOTO 10 means loop. Now I could code up my evil math wizardry and amaze all and sundry! The first equation I coded up was Leibniz. I am an AARP member as on date, so can't recollect QBasic syntax from two decades ago. However, the cool kids these days grok Scala, so here's Leibniz in Scala, using the map & reduce functions.

Leibniz 10 terms: 3.2323158

This gives you a rather poor approximation, since the convergence is sublinear.

Soon, I learnt about computing the area under a curve using an approximation called Simpson's Rule. In the Scala snippet below, simpson(..) is asked to compute the area of a "first class function" f in the interval [a,b]. Simpson says, take one sixth the interval times the sum of the function at the corners and four times the midpoint.

A candidate function is one-fourth of the circle with radius 2, under the interval [0,2].

**printf("\nSimpson on quarter circle with radius two: %.10f", simpson( x=> math.sqrt(4-x*x), 0, 2))**yields

**Simpson on quarter circle with radius two: 3.1364470653**We've certainly improved upon the Leibniz

But we could do much better if we picked a better circle.

How about one eighth of a circle with a radius sqrt(8) ? That's Pi as well.

Looking at this circle below, we find an isosceles triangle with a height and base of two. Such a triangle obviously has an area 1/2 * 2 * 2 = 2.

Now, one-eighth of the circle is the portion of the circle right above the purple line.

So that's Pi.

Now, pay attention.

If you were to find the area of this curve in the [0,2] interval, the triangle contributes 2, and the one-eighth circle contributes Pi. So, 2+Pi

But Pi and 2+Pi look exactly the same to the right of the decimal point!

So subtracting off two should give us Pi!

Note the subtraction below.

**printf("\nSimpson on one-eighth circle with radius sqrt(8): %.10f", simpson( x=> math.sqrt(8-x*x), 0, 2) - 2)**yields

**Simpson on one-eighth circle with radius sqrt(8): 3.1415918322**a much better approximation of Pi given how few computations we actually incur.

But we have one more ace up our sleeve!

Remember the Leibniz expansion ?

Well, rather than run that Taylor series over 10 terms, what if, we feed the actual arctangent function behind Leibniz to our 10-subinterval Simpson ?

So, here's how that function looks, in the [0,1] interval:

**printf("\nSimpson on arctangent: %.10f", 4*simpson( x=> 1.0/(1+x*x), 0, 1))**yields

**Simpson on arctangent: 3.1415926530**our best approximation so far, for very little work on our part!

Note: If Scala is not your style, here's the same code in C++ (0x)