Click here to Skip to main content
13,198,075 members (81,285 online)
Click here to Skip to main content
Add your own
alternative version


37 bookmarked
Posted 19 Sep 2005

Calculate pi to one million decimal places

, 19 Sep 2005
Rate this:
Please Sign up or sign in to vote.
A simple program that should take a few hours to run.



Pi is one of the most important numbers in mathematics. It is defined as the ratio of a circle's circumference to its diameter, but it crops up in all sorts of places in mathematics. It is an infinitely long non-recurring decimal number.

I'm not going to try to write pi as the Greek letter in this article, because some browsers might show it incorrectly.

Calculating lots of digits of pi

There are many ways to do this. Some methods converge rapidly but are complicated to implement. Some are simple to implement but converge very slowly. I've chosen a method that is fairly simple and converges reasonably fast. It is based on the following formula:

pi / 4 = 4 * tan-1(1 / 5) - tan-1(1 / 239)

This is Machin's formula and is exact.

tan-1() is the <A target=_blank InverseTangent.html? ?http:>Inverse Tangent function, and I use the Maclaurin series to calculate it:

tan-1(z) = z - z3 / 3 + z5 / 5 - z7 / 7 + ...

By including sufficiently many terms of this series, we can achieve any desired accuracy. To get 1,000,000 decimal places accuracy for pi, we need about 715,000 terms of the tan-1(1/5) series and about 210,000 terms of the tan-1(1/239) series, but this doesn't have to be worked out in advance, the attached program stops automatically when it determines that the required accuracy has been reached.

Multi-length arithmetic

A 32-bit integer only gives us about 9 significant digits. To get one million decimal places, I've done a simple implementation of the multi-length arithmetic operations that I need, using a big array of ints. The multi-length numbers are wrapped up inside the class CRHMultiLengthInteger. I've made full use of the operator notation in C++ to make the code look like we're just working with ordinary numbers. For example:

term5m /= n5 * 2 + 1;

looks like it's just dividing term5m by a number, but it's actually calling CRHMultiLengthInteger::operator /=().

The program

The attached program is a straightforward Win32 Console Application (it started out as the standard "Hello, World!" application with MFC support). You can adjust how many decimal places it calculates by changing #define NDecimalPlaces (1000100) to some other number. The answer is written to a file called resultNDecimalPlaces.txt.

Points of Interest

  • The algorithm described above has an O(n2) duration. So 10 times as many decimal places will take about 100 times as long. My 7,000 MIPs PC took about 15 hours to calculate the first 1,000,000 decimal places of pi. That works out at about 380 million million instructions in total.
  • To get 1,000,000 decimal places, each multi-length number is implemented as an array of 166685 integers, using a base of 1,000,000 so that each integer gives us 6 significant digits. This actually gives us 1,000,104 decimal places, but the last few might be incorrect because of the infinite number of terms of the Maclaurin series that we have to throw away. Comparison with this shows that the last three decimal places are wrong, so we have actually got pi correct to 1,000,101 decimal places.
  • The first 1,000,000 decimal places of pi were first calculated in 1973.
  • The 19th century English mathematician William Shanks spent over 15 years calculating the first 707 places of pi using Machin's formula. He published his results in 1873. In 1944 it was found that he had made a mistake in the 528th place, and all the following digits were wrong. By changing to #define NDecimalPlaces (1000), you can now achieve in a fraction of a second more than William Shanks failed to achieve in 15 years.


This article is just for fun. I know it's pretty straightforward but I thought people might be interested in seeing it.

Getting a computer to calculate lots of digits of pi does have its uses, it can be used to show that the computer, or at least its ALU, is working properly.


Thanks very much to:


  • 6th September 2005 - first submitted.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Chris Hills
Software Developer (Senior)
United Kingdom United Kingdom
I've been programming computers since about 1968. I started at school with Algol 60 on an Elliott 803. From there I progressed through the Z80 and other microprocessors to the PC, DOS and Windows, Pascal, C and C++.

My other interests include astronomy and classical music. All of my contributions to Code Project have arisen from programs I've written in these areas.

You may also be interested in...


Comments and Discussions

SuggestionPi Pictures. Pin
Eduardo N Hering29-Aug-11 14:45
memberEduardo N Hering29-Aug-11 14:45 
GeneralArticle for review Pin
ssnirgudkar21-Dec-09 15:15
memberssnirgudkar21-Dec-09 15:15 
GeneralRe: Article for review Pin
ssnirgudkar21-Dec-09 15:17
memberssnirgudkar21-Dec-09 15:17 
GeneralEnough with the term "million million"! Pin
prom 197616-May-08 0:03
memberprom 197616-May-08 0:03 
GeneralRe: Enough with the term "million million"! Pin
Chris Hills17-May-08 3:39
memberChris Hills17-May-08 3:39 
GeneralRe: Enough with the term "million million"! Pin
canozurdo25-Jun-08 8:37
membercanozurdo25-Jun-08 8:37 
QuestionRadians or Degrees? Pin
Thmath17763-May-08 10:06
memberThmath17763-May-08 10:06 
NewsOptimization to the death Pin
pouyaebad9-Mar-08 1:01
memberpouyaebad9-Mar-08 1:01 
I was surfing the 'code project' site, while I see your article about calculating PI number. I liked it so much and since my hobby is optimization I tried to write a C code to calculate it by implementing the algorithm you mentioned.
Although my program is a small one (about 350 SLOC in C) but it took me a week to write it.

My optimizations in the code include:
1- Coding the division function in assembly to get the quotient and remainder with just an instruction.
2- Converting the entire numbers to Hex base in the beginning and doing the whole calculation in Hex (which is much faster), in the end I converted result to Decimal base again.
3- Manipulating process and thread properties a bit to eat the whole CPU power.

By these tricks, I got the following result for calculating PI to the 100,000th digit:
(All ran on P4 2.6 GHz)

1- Original program written by Chris : 239 seconds
2- Optimized program by Todd : 84 seconds (see notes below)
3- My Optimized program : 52 seconds
4- My Optimized program (multi threaded): 36 seconds
5- My Optimized program (multi threaded): 24 seconds

*note: the last experiment ran on a quad core Intel CPU (3GHz) to get the advantages of multi threading feature.

Total calculation time to the 1,000,000th digit is less than 52 minutes on quad core Intel processor.

I am sure with a few changes to this code; it can run faster on 64 bit CPUs (I used 64 bit integers in my calculations)
I am also interested if there could be any further optimizations to algorithms or implementation techniques which make this program run even faster.

BTW, thank you for your state of the art article.

GeneralRe: Optimization to the death Pin
pouyaebad12-Mar-08 1:09
memberpouyaebad12-Mar-08 1:09 
GeneralRe: Optimization to the death Pin
Brian Wiegand9-Feb-09 18:12
memberBrian Wiegand9-Feb-09 18:12 
GeneralRe: Optimization to the death Pin
hashme334-Nov-09 9:21
memberhashme334-Nov-09 9:21 
QuestionWhy is this important? Pin
curiousone11-Jun-07 9:31
membercuriousone11-Jun-07 9:31 
AnswerRe: Why is this important? Pin
lancere25-Aug-07 14:12
memberlancere25-Aug-07 14:12 
AnswerRe: Why is this important? Pin
Crusiatus Black3-Nov-08 2:24
memberCrusiatus Black3-Nov-08 2:24 
GeneralRe: Why is this important? Pin
ron real30-Jul-10 12:30
memberron real30-Jul-10 12:30 
QuestionMultilength arit. Pin
diegusaldus2-Apr-07 23:05
memberdiegusaldus2-Apr-07 23:05 
AnswerRe: Multilength arit. Pin
Chris Hills8-Apr-07 10:04
memberChris Hills8-Apr-07 10:04 
GeneralIt's not Exact Pin
Zmurf7-May-06 23:55
memberZmurf7-May-06 23:55 
GeneralRe: It's not Exact Pin
Super Lloyd24-May-06 19:40
memberSuper Lloyd24-May-06 19:40 
GeneralRe: It's not Exact Pin
Zmurf25-May-06 2:24
memberZmurf25-May-06 2:24 
GeneralRe: It's not Exact Pin
Super Lloyd25-May-06 2:54
memberSuper Lloyd25-May-06 2:54 
GeneralBailey-Borwein-Plouffe formula (this is amazing) Pin
Don Clugston16-Oct-05 23:28
memberDon Clugston16-Oct-05 23:28 
Generalsimple optimizations Pin
cub4bear28-Sep-05 5:32
membercub4bear28-Sep-05 5:32 
GeneralAnother optimization ? Pin
Mr. Fox30-Sep-05 6:19
memberMr. Fox30-Sep-05 6:19 
GeneralRe: Another optimization ? Pin
cub4bear30-Sep-05 8:31
membercub4bear30-Sep-05 8:31 
Generalmy results Pin
manuelpg26-Sep-05 21:41
membermanuelpg26-Sep-05 21:41 
QuestionHow long did it take? Pin
WREY19-Sep-05 11:49
memberWREY19-Sep-05 11:49 
AnswerRe: How long did it take? Pin
Chris Maunder19-Sep-05 12:05
adminChris Maunder19-Sep-05 12:05 
GeneralRe: How long did it take? Pin
WREY19-Sep-05 12:14
memberWREY19-Sep-05 12:14 
AnswerRe: How long did it take? Pin
Chris Hills21-Sep-05 11:56
memberChris Hills21-Sep-05 11:56 
AnswerRe: How long did it take? Pin
Joe Pizzi28-Sep-05 12:01
memberJoe Pizzi28-Sep-05 12:01 
GeneralRe: How long did it take? Pin
WREY28-Sep-05 14:03
memberWREY28-Sep-05 14:03 
GeneralRe: How long did it take? Pin
Joe Pizzi28-Sep-05 16:25
memberJoe Pizzi28-Sep-05 16:25 
GeneralRe: How long did it take? Pin
WREY28-Sep-05 19:17
memberWREY28-Sep-05 19:17 
GeneralRe: How long did it take? Pin
f230-Sep-05 5:10
memberf230-Sep-05 5:10 
GeneralRe: How long did it take? Pin
lusheebabe23-Oct-08 20:06
memberlusheebabe23-Oct-08 20:06