Python Leibniz summation
<p><a href="http://i.stack.imgur.com/baHSG.png" rel="nofollow">Leibniz summation</a></p>

<p>I'm trying to get leibniz summation with python, but, with my code, I'm getting slightly different value. I can't find why it's not giving me the right answer.</p>

<pre><code>import math
def estimate_pi( iterations ):
    pi = 0.0
    for n in range(0,iterations+1):
        pi = pi + (math.pow(-1,n)/((2*n)+1))
    return pi

print("How many iterations?")
print(estimate_pi(int(input())))
</code></pre>

<p>The summation does not estimate <code>pi</code>, it estimates <code>pi/4</code>. So change the code to something like:</p>

<pre><code>def estimate_pi( iterations ):
    sum = 0.0
    for n in range(iterations):
        sum += (math.pow(-1,n)/((2*n)+1))
    # sum now estimates pi/4, so return sum * 4 ~ (pi/4) * 4 ~ pi
    return (sum * 4)

print estimate_pi(100000)
</code></pre>

<p><strong>Output:</strong></p>

<pre><code>3.14158265359
</code></pre>

python