Do you like the rotating PI symbol? I created it using Corel Dream 3D version 7. But enough of this...

## Writing Software To Calculate PI

Or rather an approximation of PI. Way back in the Summer of 1990 when I was still an undergraduate, I was watching an Open University television programme about the calculation of PI. In it they gave out some formulas and I thought I could do that on my home computer. So I wrote some code in SuperBASIC and ran it on my old, trusty Sinclair QL. Needless to say it was rather slow.

Years later in 1994, when I was just beginning to learn C on Unix, I decided to convert that old BASIC code into C. Well the code ran faster and gave the right answer, but was still slower than it should have been. Credit should go to Jeremy Riley for the speed of the current version of the code.

Well the formulas are shown below. (1) is Taylor's expansion of tan-1.

(1) tan-1(x) = x - x^3/3 + x^5/5 - x^7/7 + ...
(2) PI = 24 tan-1(1/8) + 8 tan-1(1/57) + 4 tan-1(1/239)

Using (1) and (2):

(3) PI = 24(1/8) + 8(1/57) + 4(1/239)
- ( (24(1/8)^3) + 8(1/57)^3 + 4(1/239)^3) ) / 3
+ ( (24(1/8)^3 * (1/8)^2) + (8(1/57)^3 * (1/57)^2) + (4(1/239)^3 * (1/239)^2) ) / 5
- ...
...+ ( (24(1/8)^n) + (8(1/57)^n) + (4(1/239)^n) ) / n
- ...

(3) is used by the programs available below to calculate an approximation for PI.

There are other methods for calculating PI, which converge much faster than the one given above. However this one is fairly simple to implement and as more terms are summed, each new term gets calculated faster! I wonder which one is best - a complicated method that gives you lots of decimal places for each new term summed or a simple one that only gives you a few?

## How Fast Is This Method?

approx. no. of decimal places of PI Windows95* Red-Hat Linux**
10, 000 14 secs 10 secs
100, 000 23 mins 34 secs 17 mins 10 secs

* Running on a Dell Pentium Pro 200Mhz. Microsoft Visual C++ optimizations used to maximize speed.
** Running on exactly the same computer as above. gcc used with -O3 switch.

The original program that was written in 1995 to calculate PI was on SunOS. Crude ports have been recently been to Windows and Linux. If you want you can download my software. After running the program, you have to specify how many decimal places arithmetic should be performed to. Unless you want to wait all day, it's best to start off with something like 10, 000. You should bear in mind, my programs just sum the terms and as such the last couple of decimal places for the approximation of PI should be discarded as adding more terms will change these digits.

This software comes with absolutely no warranty and I am not responsible for any damage it may cause.

Click on WINPI.ZIP to download the Windows95/NT version of my program.

Click on LINUXPI.ZIP to download the Red-Hat Linux (Intel platform only) version of my program.

Click on PI_SIT.HQX to download the Apple Mac version of my program. Click on calculatepi1.5f.sit.hqx to download the same program from the University of Michigan's Mac software archive.

If a single click does not work, try a right-click followed by Save Link As...

## What's All The Fuss About?

Here's the first 11 digits of PI in all their glory!

If you want to find out more about PI, a good place to go is the The Uselessness of Pi and its irrational friends page