The post In honor of Pi Day (3.14), approximate pi in C# uses the following sequence to approximate pi.
![[approximate pi]](http://www.csharphelper.com/howto_calculate_pi2.png)
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).
![[approximate pi]](http://www.csharphelper.com/howto_approximate_pi_gl.png)
Next the program uses the following Nilakantha series.
![[approximate pi]](http://www.csharphelper.com/howto_approximate_pi_nilakantha.png)
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.
![[approximate pi]](http://www.csharphelper.com/howto_approximate_pi_newton.png)
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.
![[approximate pi]](http://www.csharphelper.com/howto_approximate_pi_arcsine.png)
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.
![[approximate pi]](http://www.csharphelper.com/howto_approximate_pi2.png)
For more information about these series, see the Wikipedia article Approximations of ?.




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