which pi formula is given in the decimal module documentation?
Mark Dickinson
dickinsm at gmail.com
Fri Dec 11 05:30:44 EST 2009
On Dec 11, 8:16 am, Anh Hai Trinh <anh.hai.tr... at gmail.com> wrote:
> I'm just curious which formula for pi is given here: <http://
> docs.python.org/library/decimal.html#recipes>?
>
> def pi():
> """Compute Pi to the current precision.
>
> >>> print pi()
> 3.141592653589793238462643383
>
> """
> getcontext().prec += 2 # extra digits for intermediate steps
> three = Decimal(3) # substitute "three=3.0" for regular
> floats
> lasts, t, s, n, na, d, da = 0, three, 3, 1, 0, 0, 24
> while s != lasts:
> lasts = s
> n, na = n+na, na+8
> d, da = d+da, da+32
> t = (t * n) / d
> s += t
> getcontext().prec -= 2
> return +s # unary plus applies the new precision
>
> It looks like an infinite series with term `t`, where`n` = (2k-1)^2
> and `d` = d = 4k(4k+2) for k = 1... Does it have a name?
Interesting. So the general term here is
3 * (2k choose k) / (16**k * (2*k+1)), k >= 0.
Python 3.2a0 (py3k:76334:76335, Nov 18 2009, 11:34:44)
[GCC 4.0.1 (Apple Inc. build 5490)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from math import factorial as f
>>> 3*sum(f(2*k)/f(k)/f(k)/(2*k+1)/16**k for k in range(50))
3.141592653589794
I've no idea what its name is or where it comes from, though. I
expect Raymond Hettinger would know.
--
Mark
More information about the Python-list
mailing list