[Edu-sig] followup to older post... re Pi generator

Tim Peters tim.peters at gmail.com
Tue Sep 29 18:41:42 CEST 2015

Copying Guido because he's the actual original author of this code.  I
remember it being in a very early intro to Python, which I can't find
now.  And it used to be included in a Python distribution's Demo
directory.  But, e.g., you can see it in this mailing list message
from Guido in 1994, during an early discussion of what eventually
became Python's generators.  Guido, do you remember where this
algorithm came from?

    scroll down to point "6)"

[kirby urner <kirby.urner at gmail.com>]
> I'm still interested in finding more bibliographic sources for this one, not
> necessarily in Python syntax i.e. did Euler come up with this or who?
> def pi_digits():
>     k, a, b, a1, b1 = 2, 4, 1, 12, 4
>     while True:
>         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)
>             a, a1 = 10*(a%b), 10*(a1%b1)
>             d, d1 = a/b, a1/b1
> [  http://mail.python.org/pipermail/edu-sig/2012-December/010728.html ]
>>>> pi = pi_digits()
>>>> "".join([str(next(pi)) for i in range(100)]))
> '3141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067'
> ... so far what we know is some high school student handed it in for
> homework.

More information about the Edu-sig mailing list