[Edu-sig] Ramanujan's Pi (an exercise with generator & decimal features)

kirby urner kirby.urner at gmail.com
Mon Sep 4 05:31:55 CEST 2006


So here I'm using Python's new extended precision decimal type with
one of Ramanujan's crazy pi engines:

http://people.bath.ac.uk/ma2lg/project/ramanuja.htm

I also used another generator I wrote earlier to get root8 to a lot of
digits.  Here:  http://www.4dsolutions.net/ocn/python/roots.py

Factorial -- fact(n) -- is implemented back stage (easily done).

As you can see, Python does a great job getting a lot of digits.

=======================================================

from __future__ import division

from decimal import *
getcontext().prec = 100  # setting high precision standard!

def pieinsky():
    c1 = Decimal(4)
    c2 = Decimal(1103)
    c3 = Decimal(26390)
    c4 = Decimal(396)
    c5 = Decimal(9801)
    # code formatted for readability (make it be one line)
    root8 = Decimal('2.82842712474619009760337744841939615
                     7139343750753896146353359475981464956
                     9242140777007750686552831454700276')
    i = Decimal(0)
    thesum = Decimal(0)
    while True:
	term = (fact(c1*i)*(c2 + c3*i))/(pow(fact(i),4)*pow(c4,4*i))
	thesum = thesum + term
	yield 1/((root8/c5)*thesum)
	i += 1

>>> pigen = pieinsky()
>>> pigen.next()
Decimal("3.1415927300133056603139961890252155185995816071100335596565362901=
28551455441321642740854085209704228")
>>> pigen.next()
Decimal("3.1415926535897938779989058263060130942166450293228488791739637915=
05784400648511674693278393849439482")
>>> pigen.next()
Decimal("3.1415926535897932384626490657027588981566774804623347811683995956=
44739794558841580205059234965983146")
>>> pigen.next()
Decimal("3.1415926535897932384626433832795552731599742104203799112167038960=
06945787942847069061241208907980917")
>>> pigen.next()
Decimal("3.1415926535897932384626433832795028841976638181330306239761655909=
98553105507509100070309837894567968")
>>> pigen.next()
Decimal("3.1415926535897932384626433832795028841971693993798468327435125072=
81953256921043725789395219809199167")
>>> pigen.next()
Decimal("3.1415926535897932384626433832795028841971693993751058210209332424=
69729074078272131764032273329551022")
>>> pigen.next()
Decimal("3.1415926535897932384626433832795028841971693993751058209749445927=
57814327091194665469239192568221775")
>>> pigen.next()
Decimal("3.1415926535897932384626433832795028841971693993751058209749445923=
07816410719485095770076209900585536")
>>> pigen.next()
Decimal("3.1415926535897932384626433832795028841971693993751058209749445923=
07816406286209042542808018278896595")
>>> pigen.next()
Decimal("3.1415926535897932384626433832795028841971693993751058209749445923=
07816406286208998628035262302197974")
>>> pigen.next()
Decimal("3.1415926535897932384626433832795028841971693993751058209749445923=
07816406286208998628034825342121433")
>>> pigen.next()
Decimal("3.1415926535897932384626433832795028841971693993751058209749445923=
07816406286208998628034825342117070")

Stops changing at this point -- limit reached.

It's correct to last two digits, per
http://www.mathwithmrherte.com/pi_digits.htm

Kirby


More information about the Edu-sig mailing list