Let me write the code again.. Thanks for your help. On Mon, Jul 18, 2011 at 10:38 PM, James Reynolds <eire1...@gmail.com> wrote:
> > > On Mon, Jul 18, 2011 at 12:10 PM, surya k <sur...@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* >> >> _______________________________________________ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> > > > I am getting the following error : which is completely surprising! > > > You only define "series" when nu/de == XXX. Just don't print series outside > of where it is defined, or print it outside of the while loop if you can be > assured it is defined when the while loop ends. > > Even so, you would still end up with the overflow error, because you have > de / nu and I'm assuming you want nu / de. As an aside, you might want to > consider creating a variable above all the if statements called nude = nu / > de and replace all of the nu / de 's to nude. This way you only divide once > and not (potentially) three times per loop. > > > > > > > > > > >
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor