The post In honor of Pi Day (3.14), approximate pi in C# uses the following sequence to approximate pi.

That sequence is elegant but unfortunately it converges very slowly so you need to take a lot of terms to get any kind of precision. For example, 1,000 terms only gives you a value of π that differs from the correct value by less than 0.001.

This example uses several different sequences to approximate pi. First it uses the following Gregory-Leibniz sequence (also used by the previous example).

Next the program uses the following Nilakantha series.

This series is considerably more complicated than the Gregory-Leibniz series but it converges much more quickly.

Next the program uses the following series by Newton.

Here n!! means the product of the odd numbers up to n so n!! = 1 · 3 · 5 … n.

The picture at the top of the post makes it look like this series converges more slowly than the Nilakantha series, but it actually converges faster after you take a few more terms.

Next the program uses the following series based on the Arcsine function.

Finally the program calculates 355 / 113, which is remarkably close to π, matching it for seven decimal places! It turns out that this is the best rational approximation you can get with a denominator less than 30,000.

Here’s the program displaying values calculated with 20 terms. In this picture you can see that the Newton series gives better results than the Nilakantha series. Remarkably only the Arcsine series gives a better result than the single term 355 / 113 after 20 terms.

For more information about these series, see the Wikipedia article Approximations of ?.

##
About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.

Pingback: Graph several pi approximations in C# - C# HelperC# Helper