looping over product to compute a serie in python
<p>I'm just gonna compute the result of below serie in python:</p>

<p>The formula</p>

<p><a href="http://i.stack.imgur.com/vxNE0.gif" rel="nofollow"><img src="http://i.stack.imgur.com/vxNE0.gif" alt="enter image description here"></a></p>

<p>So, here is my function to compute:</p>

<pre><code> def compute(limit):
     pi = 1
     for i in range(1,limit):
         pi = pi * ((4*(i**2))//(4*(i**2)-1))
     print(2*pi)

 compute(10000) /*returns 2*/
</code></pre>

<p>I know it is a silly question. But could you address the problem with this snippet?</p>

<p>As others have already mentioned, <code>//</code> is integer division. No matter whether you're dividing floats or integers, the result will always be an integer. Pi is not an integer, so you should use float division: <code>/</code> and tell Python explicitly that you really want a float by converting one of the numbers to float<sup>*</sup>. For example, you could do <code>4</code> -> <code>4.</code> (notice the dot).</p>

<p>You could do the same thing in a more clear way using <a href="https://docs.python.org/3/library/functools.html" rel="nofollow"><code>functools</code></a> and <a href="https://docs.python.org/2/library/operator.html" rel="nofollow"><code>operator</code></a> modules and a generator expression. </p>

<pre><code>import functools 
import operator

def compute(limit):
    return 2 * functools.reduce(operator.mul, ((4.*(i**2)/(4*(i**2)-1) for i in range(1, limit + 1))
</code></pre>

<p><sub>* Python 3 does float division with <code>4/(something)</code> even without this, but Python 2.7 will need <code>from __future__ import division</code> to divide as freely as Python 3, otherwise the division will result in an integer in any way.</sub></p>

python