Finding an approximation of the constant Pi within error [ PYTHON ]
<p>I just started learning Python and I am having a problem writing the function.</p>

<p>The following is an inﬁnite series that calculates an approximation of π :
π = 4/1 − 4/3 + 4/5 - 4/7 + 4/9 - 4/11 ... </p>

<p>I am trying to write a function that takes as a parameter a floating point value error and approximates the constant π within error by computing the above sum, term by term, until the absolute value of the difference between the current sum and the previous sum (with one fewer terms) is no greater than error. Once the function finds that the difference is less than error, it should return the new sum.</p>

<p>The following shows the execution of this function on some examples:</p>

<pre><code>&gt;&gt;&gt; aprPi(0.01)
3.1465677471829556
&gt;&gt;&gt; aprPi(0.0000001)
3.1415927035898146
</code></pre>

<p>I still don't know how to compute it. Can someone help me?</p>

<p>This is what I have so far:</p>

<pre><code>def aprPi(err):
    first  = 4/test(0) - 4/test(1)
    second = first + 4/test(2) - 4/test(3)
    n=4
    while abs(first - second) &gt; err:
        first = second
        second = second + test(n)
        n +=1
    return second


def test(n):
    sum = 1
    for i in range(n):
        sum += 2

    return sum
</code></pre>

<p>Thank you</p>
