11,413,676 members (60,032 online)

# Calculate pi to one million decimal places

, 19 Sep 2005
 Rate this:
A simple program that should take a few hours to run.

## Introduction

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 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 `int`s. 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.

## Why?

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.

## Acknowledgments

Thanks very much to:

## History

• 6th September 2005 - first submitted.

A list of licenses authors might use can be found here

## Share

Software Developer (Senior)
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.

 First PrevNext
 Pi Pictures. Eduardo N Hering, 29-Aug-11 15:45 Eduardo N Hering 29-Aug-11 15:45
 Article for review ssnirgudkar, 21-Dec-09 16:15 ssnirgudkar 21-Dec-09 16:15
 Re: Article for review ssnirgudkar, 21-Dec-09 16:17 ssnirgudkar 21-Dec-09 16:17
 Enough with the term "million million"! prom 1976, 16-May-08 1:03 prom 1976 16-May-08 1:03
 The term "a million million" is easily quantified by a 2nd-grader as a "trillion"...For christ's sake, we don't say, "a thousand thousand" to describe "a million", unless I am 14 centuries ahead of my time and I didn't know it... Let me quantify numerics by name: Number of zeros/U.S. & scientific community 3/thousand 6/million 9/billion 12/trillion 15/quadrillion 18/quintillion 21/sextillion 24/septillion 27/octillion 30/nonillion 33/decillion 36/undecillion 39/duodecillion 42/tredecillion 45/quattuordecillion 48/quindecillion 51/sexdecillion 54/septendecillion 57/octodecillion 60/novemdecillion 63/vigintillion 66/undecillion 303/centillion etc... Those who even speak "million million" are simply retarded and do not know how to quantify numbers higher than a million...The same applies for those who say "billion billion", who cannot quantify a quintillion. To salvage any intelligence, at least describe it to the tenth power, instead of thinking that saying the same word twice somehow multiplies it's meaning. This is what lack of funding does to american education (now a contradiction in terms).
 Re: Enough with the term "million million"! Chris Hills, 17-May-08 4:39 Chris Hills 17-May-08 4:39
 Re: Enough with the term "million million"! canozurdo, 25-Jun-08 9:37 canozurdo 25-Jun-08 9:37
 Radians or Degrees? Thmath1776, 3-May-08 11:06 Thmath1776 3-May-08 11:06
 Re: Optimization to the death Brian Wiegand, 9-Feb-09 19:12 Brian Wiegand 9-Feb-09 19:12
 Re: Optimization to the death hashme33, 4-Nov-09 10:21 hashme33 4-Nov-09 10:21
 Why is this important? curiousone, 11-Jun-07 10:31 curiousone 11-Jun-07 10:31
 Re: Why is this important? lancere, 25-Aug-07 15:12 lancere 25-Aug-07 15:12
 Re: Why is this important? Crusiatus Black, 3-Nov-08 3:24 Crusiatus Black 3-Nov-08 3:24
 Re: Why is this important? ron real, 30-Jul-10 13:30 ron real 30-Jul-10 13:30
 Multilength arit. diegusaldus, 3-Apr-07 0:05 diegusaldus 3-Apr-07 0:05
 Re: Multilength arit. Chris Hills, 8-Apr-07 11:04 Chris Hills 8-Apr-07 11:04
 It's not Exact Zmurf, 8-May-06 0:55 Zmurf 8-May-06 0:55
 Re: It's not Exact Super Lloyd, 24-May-06 20:40 Super Lloyd 24-May-06 20:40
 Re: It's not Exact Zmurf, 25-May-06 3:24 Zmurf 25-May-06 3:24
 Re: It's not Exact Super Lloyd, 25-May-06 3:54 Super Lloyd 25-May-06 3:54
 Bailey-Borwein-Plouffe formula (this is amazing) Don Clugston, 17-Oct-05 0:28 Don Clugston 17-Oct-05 0:28
 simple optimizations cub4bear, 28-Sep-05 6:32 cub4bear 28-Sep-05 6:32
 Another optimization ? Mr. Fox, 30-Sep-05 7:19 Mr. Fox 30-Sep-05 7:19
 Re: Another optimization ? cub4bear, 30-Sep-05 9:31 cub4bear 30-Sep-05 9:31
 Last Visit: 31-Dec-99 19:00     Last Update: 26-Apr-15 22:36 Refresh 12 Next »