Find the number of terms required in the Leibniz formula to approximate pi to n significant figures?
<p>I have to find the number of terms required to approximate pi to n significant figures using the sum of the Leibniz series. I have already found the sum and the approximation of pi, but I don't know how to start writing the function that compares sigfigs in the two variables, or even how to determine the number of sigfigs in a given number. Any help would be appreciated, thanks.</p>

<pre><code>sum = 0
for i in range(800001):
    int = ((-1)**i) / (2*i+1)
    sum += int 
print(sum) 
pi = sum*4
</code></pre>

<p>print(pi)</p>

<p>Because of the context of this question, I suspect you are not actually interested in knowing how to check for the least significant digits, but rather to know when your approximation is '<em>good enough</em>'.</p>

<p>When approximating any value by computing the sum of a sequence, the easiest way to terminate your calculation is to improve the answer until it is close enough so that its square (or absolute value) differs from the last term by less than a predetermined tolerance.</p>

<p>A good way to do this is to check, rather than using a <code>for</code> loop, to use a <code>while</code> loop that checks if the answer differs from the previous answer by this aforementioned tolerance. </p>

python loops pi