This is what mine look like:
http://postimage.org/image/rsqnz28tn/
16 Replies - 695 Views - Last Post: 02 October 2012 - 01:17 PM
#17
Re: Compute pi by using Gregory series
Posted 02 October 2012 - 01:17 PM
Hmm, was unaware integer math was no longer integer math in Python 3. Thanks for the info.
Here's how it would work in either system:
I prefer the iterative approach.
So, by way of example:
Here's how it would work in either system:
>>> def pi1():
... sum = 0.0
... for i in range(1, 100000 +1):
... n = ( (-1) ** (i + 1) / (2.0 * i - 1) )
... sum += n * 4.0
... if(i % 10000 == 0):
... print("pi is {0}".format(sum))
...
>>>
>>> pi1()
pi is 3.14149265359
pi is 3.14154265359
pi is 3.14155932026
pi is 3.14156765359
pi is 3.14157265359
pi is 3.14157598692
pi is 3.14157836788
pi is 3.14158015359
pi is 3.14158154248
pi is 3.14158265359
>>>
I prefer the iterative approach.
So, by way of example:
>>> def pi2():
... sum, n, d = 1.0, 1.0, 1.0
... for i in range(1, 100000 +1):
... n, d = -n, d + 2.0
... sum += n / d
... if(i % 10000 == 0):
... print("pi is {0}".format(sum * 4))
...
>>>
>>> pi2()
pi is 3.14169264359
pi is 3.14164265109
pi is 3.14162598581
pi is 3.14161765296
pi is 3.14161265319
pi is 3.14160931998
pi is 3.1416069391
pi is 3.14160515343
pi is 3.14160376458
pi is 3.14160265349
>>>
|
|

New Topic/Question
Reply




MultiQuote



|