<div dir="ltr"><div><div>Looking back:<br><br><a href="https://mail.python.org/pipermail/edu-sig/2006-September/006963.html">https://mail.python.org/pipermail/edu-sig/2006-September/006963.html</a><br><br></div>shows me still importing __division__ and relying on some factorial off state.<br><br></div>Tonight, preparing for class, I'm able to rely on math.factorial, introduce other improvements.<br><span style="font-family:monospace,monospace"><br># -*- coding: utf-8 -*-<br>"""<br>Created on Thu Dec  3 16:40:46 2015<br><br>See:  <a href="https://crypto.stanford.edu/pbc/notes/pi/ramanujan.html">https://crypto.stanford.edu/pbc/notes/pi/ramanujan.html</a><br><br>@author: kurner<br><br>LAB:<br><br>Write a unittest to confirm convergence to:<br><br>'3.141592653589793238462643383279502884197169<br>39937510582097494459230781640628620899862803<br>48253421170'<br><br>after n steps (this is the answer key version)<br><br>"""<br><br>import unittest<br><br>from decimal import *<br>from math import factorial as fact<br><br>def pieinsky():<br>    c1 = Decimal(4)<br>    c2 = Decimal(1103)<br>    c3 = Decimal(26390)<br>    c4 = Decimal(396)<br>    c5 = Decimal(9801)<br>    # code formatted for readability (make it be one line)<br>    root8 = Decimal('2.82842712474619009760337744841939615'<br>                     '7139343750753896146353359475981464956'<br>                     '9242140777007750686552831454700276')<br>    i = Decimal(0)<br>    thesum = Decimal(0)<br>    while True:<br>        term = (fact(c1*i)*(c2 + c3*i))/(pow(fact(i),4)*pow(c4,4*i))<br>        thesum = thesum + term<br>        yield 1/((root8/c5)*thesum)<br>        i += 1<br><br>class TestPi(unittest.TestCase):<br>    <br>    def test_outcome(self):<br>        expected = ('3.141592653589793238462643383'<br>        '2795028841971693993751058209749445923078'<br>        '164062862089986280348253421170') <br>        with localcontext() as c:<br>            c.prec = 100<br>            the_gen = pieinsky()<br>            for _ in range(20):<br>                next(the_gen)<br>    <br>        self.assertEqual(str(next(the_gen))[:len(expected)], expected)<br>        <br>if __name__ == "__main__":<br>    unittest.main()<br><br>        <br></span><br></div>