GateKiller - 11 months ago 80

C# Question

How can I calculate the value of PI using C#?

I was thinking it would be through a recursive function, if so, what would it look like and are there any math equations to back it up?

I'm not too fussy about performance, mainly how to go about it from a learning point of view.

Answer Source

If you want recursion:

```
PI = 2 * (1 + 1/3 * (1 + 2/5 * (1 + 3/7 * (...))))
```

This would become, after some rewriting:

```
PI = 2 * F(1);
```

with F(i):

```
double F (int i) {
return 1 + i / (2.0 * i + 1) * F(i + 1);
}
```

Isaac Newton (you may have heard of him before ;) ) came up with this trick. Note that I left out the end condition, to keep it simple. In real life, you kind of need one.