I Calculate the Digits of Pi – Very Slowly

Years ago I wrote an assembly language routine that drew circles with  a pen plotter.  This entailed approximating the arc of a circle with a series of small horizontal and vertical steps.  I was proud of the method because its critical loop used only additions and multiplications by 2 (bit shift left in assembly language) and was very fast.

So I thought this summer that if I can draw a circle fast, I ought to be able to calculate \pi fast.  I wrote a routine in the Python programming language, optimized it as best I could and here are my results.   (My method also allowed me to calculate \sqrt {2} – also very slowly)

Calculating Pi Data

Here are two python program fragments that show the algorithm.  The first is the original version which was written for clarity to stay close to the mathematics and the second was optimized for speed – not that it helped much.

Pi Calculation - First Try

Pi Calculation – “Optimized”

It is fairly easy to see where the error comes from.  By staying just outside of the circle – at most one vertical or horizontal step, the area is overestimated by at most 2\pi r\sqrt{2}.  The calculated area is at most \pi r^2+2\pi r\sqrt{2}.  Dividing by r^2 gives \pi + \frac{2\pi\sqrt{2}}{r}.  Increasing r ten times moves the decimal point over one.

My discovery of “drawing” conic sections by looking only one step ahead was satisfying and at least years ago useful.   I was sobered by the sad slow algorithm that I devised to calculate \pi.  A method with linear efficiency is really slow.  I now have more than a theoretical understanding of this fact.

Advertisements

About jrh794

I am a sixty-five year old math instructor at Southern Oregon University. I taught at the College of the Siskiyous in Weed California for twenty-six years. Prior to that I worked as a computer programmer, carpenter and in various other jobs. I graduated from Rice University in 1967 and have a MS in Operations Research from Stanford. In the past I have hand-built a stone house and taken long solo bicycle tours. Now I ride my mountain bike and play golf for recreation.
This entry was posted in Cool Ideas, Math Explorations. Bookmark the permalink.

One Response to I Calculate the Digits of Pi – Very Slowly

  1. Nick Chura says:

    It’s pretty cool to have this virtual-Hatton. Keep up the great posts.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s