[Tutor] getting error while solving a series that estimates the value of pi
Andre Engels
andreengels at gmail.com
Mon Jul 18 18:59:05 CEST 2011
On Mon, Jul 18, 2011 at 6:10 PM, surya k <suryak at live.com> wrote:
> Hi,
>
> The problem is to estimate the value of pi using the following series.
> *1 / pi = (( 2 * sqrt(2) )/ 9801 ) * SIGMA of k[ (4k)! (1103 + 26390*k)
> / (k!^ 4 ) * 396^(4k) ]*
> *where k is [0, infinity)*
> * Problem is located at : ThinkPython Book, www.thinkpython.org
> *Pg 89, Exercise 7.5, Think Python Book. *
> *
> *
> the series should be considered till the last term of the sigma must be <
> 1e-15
> I have written the below code :
>
> *# program estimates the value of pi*
> *# Ramanujan's series... *
> *import math*
> *
> *
> *def fact(n) : # def of factorial function.*
> * if n > 0 : *
> * return n * fact(n-1)*
> * if n == 0 :*
> * return 1*
> *
> *
> *k = 0*
> *tot = 0*
> *temp1 = 0*
> *while k >= 0 and temp1 == 0 :*
> *
> *
> * a = ( 2 * math.sqrt(2) ) / 9801 # first term before sigma *
> * nu = fact (4*k) * (1103 + (26390*k) ) # numerator of series*
> * de = pow( fact(k), 4 ) * pow ( 396, 4*k ) # denominator of series*
> * *
> * if de / nu > 1e-15 : *
> * temp = nu / de*
> * tot = tot + temp*
> * temp1 = 0 *
> * k = k + 1 *
> * elif de / nu == 1e-15 :*
> * series = a * tot *
> * k = k + 1*
> * temp1 = 0*
> * elif de / nu < 1e-15 :*
> * print series*
> * temp1 = 1*
>
>
> I am getting the following error : which is completely surprising!
>
> *Traceback (most recent call last):*
> * File "pi.py", line 30, in <module>*
> * print series*
> *NameError: name 'series' is not defined*
>
> *
> *
> Thus I have removed name 'series' and replaced with *a*tot, *
> now I am getting this error
>
> *Traceback (most recent call last):*
> * File "pi.py", line 30, in <module>*
> * print 1 / (a * tot)*
> *ZeroDivisionError: float divisio*n
>
>
>
> Thus I changed the first highlighted lines to
>
> *nu = float (fact (4*k) * (1103 + (26390*k) ) )*
> * de = float (pow( fact(k), 4 ) * pow ( 396, 4*k )) *
>
>
> now I am getting
>
> * Traceback (most recent call last):*
> * File "pi.py", line 18, in <module>*
> * de = float (pow( fact(k), 4 ) * pow ( 396, 4*k ) )*
> *OverflowError: long int too large to convert to float*
>
> *help me out of this problem, how could I fix this*
>
>
You have the definitions of numerator and denominator switched.
--
André Engels, andreengels at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110718/6c949564/attachment-0001.html>
More information about the Tutor
mailing list