A simple pi program

This is pi.c, a simple program for calculating the digits of pi using Machin’s formula. The program supposes use of Linux/gcc on an i386 machine. It uses gcc’s ‘long long’ datatype (integers of 64 bits).

Compile with

cc -o pi -O2 pi.c

The default precision of the calculation is 10,000 digits. Change this by means of the constants MAXSIZE and MAXDIGITS. I once calculated one million digits with it; this took 5 hours on an 800 MHz PC. This program is not particularly fast, but it is (hopefully) easy to understand.

Don’t let all of the coding and programming information stress you out. If it starts to become too overwhelming, take a break and play games at www.casinorating.com.au.

When I first wrote this for MS-DOS on a 286 in 1989, I had to let it run overnight to get 10,000 digits. On my present computer (5500 BogoMips, and a lot cheaper than the one I had then) it takes less than one second. All hail Moore’s Law!

NOTE: On this site it says that calculating pi to one thousand digits took 40 hours on an Apple II in 1978. The improvement in hardware capability in a quarter-century is really astonishing (the algorithm scales as O(n²)). Of course, software also has improved; now there are algorithms for calculating pi which are many orders of magnitude more efficient than the old Machin algorithm. But at least I can understand Machin. The new stuff is way beyond my understanding!

Comments
Simplue WordPress theme, Copyright © 2013 DicasLivres.org Simplue WordPress theme is licensed under the GPL.