tags:

views:

421

answers:

2

Note: I've already read this topic, but I don't understand it and it doesn't provide a solution I could use. I'm terrible with number problems.

What's a simple way to generate Pi to what number of decimals a user wants? This isn't for homework, just trying to complete some of the projects listed here:

Link

+1  A: 

The topic your talking about calculate the value of PI using the taylor series. Using the function "double F (int i)" wrote on that topic will give you the value of PI after "i" terms.

This way of calculating PI is kind of slow, i suggest you to look at the PI fast algorithm.

You can also find one implementation here that get the calculate PI to the n th digit.

Good luck!

Pierre-Luc Champigny
+3  A: 

A classic algorithm for calculating digits of pi is the Gauss-Legendre algorithm. While it is not as fast as some of the more modern algorithms it does have the advantage of being understandable.

Let

a_0 = 1
b_0 = 1/Sqrt(2)
t_0 = 1/4
p_0 = 1

Then

a_(n+1) = (a_n + b_n) / 2
b_(n+1) = Sqrt(a_n * b_n)
t_(n+1) = t_n - p_n * (a_n - a_(n+1))^2
p_(n+1) = 2 * p_n

Then

pi =. (a_n + b_n)^2 / (4 * t_n)

Here (=. means "approximately equal to") This algorithm exhibits quadratic convergence (the number of correct decimal places doubles with each iteration).

I'll leave it to you to translate this to C# including discovering an arbitrary-precision arithmetic library.

Jason