[Edu-sig] generate digits of pi

Kirby Urner kurner at oreillyschool.com
Sat Dec 22 23:33:06 CET 2012


I'm taking the liberty of reposting this generator supplied by Pythonista
michel paul on Math Future.  He's not the author though.

def pi_digits(n):
    k, a, b, a1, b1 = 2, 4, 1, 12, 4
    while n>0:
         p, q, k = k*k, 2*k+1, k+1
        a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
        d, d1 = a/b, a1/b1
        while d == d1:
            yield int(d)
            n -= 1
            a, a1 = 10*(a%b), 10*(a1%b1)
            d, d1 = a/b, a1/b1



More context:

https://groups.google.com/d/msg/mathfuture/LA0pMPC6-HE/MBGWxn4ENsUJ

Kirby
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20121222/665df230/attachment.html>


More information about the Edu-sig mailing list