[Edu-sig] followup to older post... re Pi generator
Tim Peters
tim.peters at gmail.com
Tue Sep 29 19:47:53 CEST 2015
Thanks! It's pretty clearly using a generalized continued fraction
algorithm converging to pi, but I'm not clear on which - it's
excruciatingly clever ;-)
Here are some possibilities for those motivated enough to dig into it:
https://en.wikipedia.org/wiki/Continued_fraction#Generalized_continued_fraction
On Tue, Sep 29, 2015 at 12:23 PM, Guido van Rossum <guido at python.org> wrote:
> I adapted this from an ABC program by Lambert Meertens. I don't know where
> he got it from, but I wouldn't be surprised if he came up with it himself.
> The algorithm basically shows off rational arithmetic in either language.
>
> On Tue, Sep 29, 2015 at 10:03 AM, kirby urner <kirby.urner at gmail.com> wrote:
>>
>>
>> Wow, I never even guessed Guido had anything to do with this code. The
>> world shrinks again!
>>
>> Fun!
>>
>> Kirby
>>
>>
>>
>> On Tue, Sep 29, 2015 at 9:41 AM, Tim Peters <tim.peters at gmail.com> wrote:
>>>
>>> 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?
>>>
>>> http://legacy.python.org/search/hypermail/python-1994q2/0429.html
>>> 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.
>>
>>
>
>
>
> --
> --Guido van Rossum (python.org/~guido)
More information about the Edu-sig
mailing list