Take the 2-minute tour ×

Are there any simple methods for calculating the digits of $\pi$? Computers are able to calculate billions of digits, so there must be an algorithm for computing them. Is there a simple algorithm that can be computed by hand in order to compute the first few digits?

share|improve this question
I've removed the tag [constant]. –  user126 Jul 21 '10 at 8:44
I added the tag "pi." It seems like the sort of thing we should expect lots of questions about here... –  Noah Snyder Jul 21 '10 at 16:50
My first downvote! How exciting :) –  e.James Jul 22 '10 at 13:24
Since it hasn't been mentioned yet, I would recommend the interested to look at Borwein and Borwein's "Pi and the AGM": amazon.com/dp/047131515X –  Guess who it is. Sep 4 '10 at 0:39
If it's only the first few digits, try wolframalpha.com/input/?i=pi+to+1000+digits –  lhf Sep 4 '10 at 3:42

7 Answers 7

up vote 13 down vote accepted

One of the simplest is the series for arctangent:

$$\tan^{-1}x = x - \frac{x^3}{3} + \frac{x^5}{5} - \frac{x^7}{7} \ldots$$

$\pi/4 = \tan^{-1} 1$, but that converges slowly.

You can use the addition formula for the tangent $(\tan (a+b) = (\tan a + \tan b) / (1 - \tan a \tan b))$ to break down $\pi/4$ to the sum of two angles and repeat; this can be used to come up with values for the arctangent that are smaller than 1 and therefore converge faster.

exercise for the reader: show that $\pi/4 = 2 \tan^{-1} 2/11 + 3 \tan^{-1} 1/7$

This then means that $\pi = 8 \tan^{-1} 2/11 + 12 \tan^{-1} 1/7$

Use the arctangent formula with only a few terms and you will have pi to quite a few decimal places. (3 terms yields 3.14160, 4 terms yields 3.1415924, 5 terms yields 3.1415926592)

share|improve this answer
For casual readers: Jason here employed a variant of Machin's formulae: en.wikipedia.org/wiki/Machin-like_formula –  Guess who it is. Sep 4 '10 at 0:36
@JM is there any way to determine the best two term split that results in a faster convergence toward pi? –  Eliseo d'Annunzio Nov 9 '14 at 12:02
that's easy: since the arctangent is a power series, convergence is dictated by the largest argument to the arctangent. In my case, it's 2/11. –  Jason S Nov 9 '14 at 15:26

Some of the easiest algorithms to use are the spigot algorithms. They allow you to jump rapidly to the nth digit without computing the n-1 digits before. The catch is that they only really work in binary. Here are some implementations in Python. You can see how short they are.

share|improve this answer
+1 for spigot algorithms link. –  Jason S Aug 2 '10 at 13:35
If I recall correctly, you calculate the $nth$ digit by first computing a fairly accurate approximation of the previous $\log(n)$ digits using recently discovered summations. –  DanielV Jan 26 at 0:35

Check out the questions tagged 'pi' at Stack Overflow. Many of them discuss simple numerical methods for calculating any number of digits of pi.

share|improve this answer

There is also the option of approximating $\pi$ using Monte Carlo integration.

The idea is this: If we agree that the area of a circle is $\pi r^2$, for simplicity we build a circle of area $\pi$ by setting $r=1$. Placing this circle wholly inside of another region of known area, preferably by inscribing it in a square of side length 2, then we have a ratio of the circle's area to the total area of the square (in this example that ratio is $\frac{\pi}{4}$).

The Monte Carlo method works by approximating areas based on the ratio of the number of sample points lying within our region of interest and the total number of sample points we choose to try. If we spread a uniformly distributed sequence of $N$ points over our square of area 4, and call the number of points that land inside of the inscribed circle $p$, then we can say $\frac{\pi}{4} = \frac{p}{N}$.

My implementation of this in Matlab requires tens of thousands of test points to achieve 3.14159xxxx, but I have not tried it for low-discrepancy sequences, or any other uniformly distributed point sets.

share|improve this answer

Check out the wiki page on approximations to pi, http://en.wikipedia.org/wiki/Numerical_approximations_of_%CF%80#20th_century

I really love Ramanujan's method, which I recall converges extremely rapidly.

share|improve this answer

Since the AGM hasn't been mentioned yet (apart from my plug to read the Borwein and Borwein book), here's how to use the AGM to compute $\pi$:

until |c| is small enough

which exploits the Legendre relation.

share|improve this answer

The first method that I applied successfully with function calculator was approximation of circle by $2^k$-polygon with approximating sides with one point on the circle and corners outside the circle. I started with unit circle that was approximated by square and the equation $\tan(2^{-k} \pi/4) \approx 2^{-k} \pi/4$, that gives $\pi \approx \frac{8}{2} = 4$ for $k=0$. I iterated the formula of tangent of half angle, that I solved applying the formula of the solution of second order equation, that was applied to the sum formula of tangent. I obtained the sequence $\pi \approx 8 \cdot 2^k \tan(2^{-k} \pi /4)/2$.

The problem is that the solution formula of the second order equation has square root, that is difficult to calculate by hand. That's why I kept on searching a simple approximation method that applies addition, substraction, multiplication and division of integers. I ended up to the following calculation. This method applies Machin-like formula and was first published by C. Hutton. \begin{eqnarray} \pi & = & 4 \frac{\pi}{4} = 4 \arctan(1) = 4 \arctan\Bigg(\frac{\frac{5}{6}}{\frac{5}{6}}\Bigg) = 4 \arctan\Bigg(\frac{\frac{1}{2}+\frac{1}{3}}{1-\frac{1}{2}\frac{1}{3}}\Bigg) \\ & = & 4 \arctan\Bigg(\frac{\tan(\arctan(\frac{1}{2}))+\tan(\arctan(\frac{1}{3}))}{1-\tan(\arctan(\frac{1}{2}))\tan(\arctan(\frac{1}{3}))}\Bigg) \\ & = & 4 \arctan\Big(\tan\Big(\arctan\Big(\frac{1}{2}\Big)+\arctan\Big(\frac{1}{3}\Big)\Big)\Big) \\ & = & 4 \Big(\arctan\Big(\frac{1}{2}\Big)+\arctan\Big(\frac{1}{3}\Big)\Big) \\ & = & 4 \Big(\Big\vert_0^\frac{1}{2} \arctan(x) + \Big\vert_0^\frac{1}{3} \arctan(x)\Big) \\ & = & 4 \bigg(\int_0^\frac{1}{2} \frac{1}{1+x^2} dx + \int_0^\frac{1}{3} \frac{1}{1+x^2} dx\bigg) \\ & = & 4 \bigg(\int_0^\frac{1}{2} \sum_{k=0}^\infty (-x^2)^k dx + \int_0^\frac{1}{3} \sum_{k=0}^\infty (-x^2)^k dx \bigg) \\ & = & 4 \bigg(\sum_{k=0}^\infty \int_0^\frac{1}{2} (-x^2)^k dx + \sum_{k=0}^\infty \int_0^\frac{1}{3} (-x^2)^k dx \bigg) \\ & = & 4 \bigg(\sum_{k=0}^\infty \int_0^\frac{1}{2} (-1)^k x^{2k} dx + \sum_{k=0}^\infty \int_0^\frac{1}{3} (-1)^k x^{2k} dx \bigg) \\ & = & 4 \bigg(\sum_{k=0}^\infty \bigg\vert_0^\frac{1}{2} \frac{(-1)^k}{2k+1} x^{2k+1} + \sum_{k=0}^\infty \bigg\vert_0^\frac{1}{3} \frac{(-1)^k}{2k+1} x^{2k+1} \bigg) \\ & = & 4 \bigg(\sum_{k=0}^\infty \frac{(-1)^k}{2k+1} \frac{1}{2^{2k+1}} + \sum_{k=0}^\infty \frac{(-1)^k}{2k+1} \frac{1}{3^{2k+1}} \bigg) \\ & = & 4 \sum_{k=0}^\infty \frac{(-1)^k}{2k+1} \bigg(\frac{1}{2^{2k+1}} + \frac{1}{3^{2k+1}}\bigg) \\ & = & \sum_{k=0}^\infty \frac{4(-1)^k}{2k+1} \bigg(\frac{1}{2^{2k+1}} + \frac{1}{3^{2k+1}}\bigg). \end{eqnarray} It is the most beautiful in practice numerically applicable method I have found so far.

share|improve this answer

protected by t.b. Jul 29 '11 at 11:30


Would you like to answer one of these unanswered questions instead?
