Menu

[r3313]: / trunk / py4science / book / examples / WallisPi.py  Maximize  Restore  History

Download this file

22 lines (17 with data), 442 Bytes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
from __future__ import division
from math import pi
def WallisPi(n,longform=False):
"""Compute pi using n terms of Wallis' product.
If longform is true, the result is returned as a long integer of the
form 314..."""
num = 1L
den = 1L
for i in xrange(1,n+1):
tmp = 4*i*i
num *= tmp
den *= tmp-1
if longform:
order = len(str(num))+1
return long(10**order)*2*num//den
else:
return 2.0*(num/den)
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.