looping over product to compute a serie in python
Question: I'm just gonna compute the result of below serie in python:

The formula

[![enter image description
here](http://i.stack.imgur.com/vxNE0.gif)](http://i.stack.imgur.com/vxNE0.gif)

So, here is my function to compute:

    
    
     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*/
    

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


Answer: As others have already mentioned, `//` 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: `/` and tell Python
explicitly that you really want a float by converting one of the numbers to
float*. For example, you could do `4` -> `4.` (notice the dot).

You could do the same thing in a more clear way using
[`functools`](https://docs.python.org/3/library/functools.html) and
[`operator`](https://docs.python.org/2/library/operator.html) modules and a
generator expression.

    
    
    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))
    

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

