[Edu-sig] generate digits of pi

kirby urner kirby.urner at gmail.com
Mon Dec 24 01:35:10 CET 2012


Right, there's like a "stutter" in the inner while loop where it sometimes
spits out more digits before getting back to the outer loop, so sometimes
you get one or two more digits than requested.

That doesn't mean I understand the algorithm, i.e. why d == d1 is critical.

Kirby


On Sun, Dec 23, 2012 at 3:49 PM, michel paul <pythonic.math at gmail.com>wrote:

> I realized something. This was the original version:
>
> 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
>
>
> I forget where it came from. Like I had mentioned, I had a really bright
> student awhile back who was really intrigued by this, and he at one point
> edited it to produce the digits in binary. In the original form the
> generator never terminates. Somewhere along the line an edit was made to
> try to get it to terminate at n digits. Probably to make calling it easy to
> call as in list(pi_digits(n)).
>
> - Michel
>
>
> On Sat, Dec 22, 2012 at 5:34 PM, Kirby Urner <kurner at oreillyschool.com>wrote:
>
>> Got it, no wrong digits just not always exactly the number you asked for.
>>
>> This happens often in 3.2 as well:
>>
>> >>> exp = ((n,len(list(pi_digits(n)))) for n in range(10000))  # (number
>> asked, number got)
>> >>> exp2 = ((a,b) for a,b in exp if a != b)  # filter on "not same"
>> >>> for i in range(10): print(next(exp2), end=", ")
>> (2, 3), (4, 5), (10, 11), (16, 17), (18, 19), (22, 23), (28, 31), (29,
>> 31), (30, 31), (34, 36),
>>
>> Kirby
>>
>>
>>
>> On Sat, Dec 22, 2012 at 4:40 PM, <david at handysoftware.com> wrote:
>>
>>> In each case I asked for only 79 digits, but got 79, 82, and 83 digits
>>> depending on whether I was using python 3.2, python 2.6, or python 2.6 with
>>> -Qnew, respectively. The digits all seem to be correct, but the algorithm
>>> for stopping at digit n seems to be very sensitive.
>>>
>>>
>>>
>>> David H
>>>
>>>
>>>
>>
>> _______________________________________________
>> Edu-sig mailing list
>> Edu-sig at python.org
>> http://mail.python.org/mailman/listinfo/edu-sig
>>
>>
>
>
> --
> ==================================
> "What I cannot create, I do not understand."
>
> - Richard Feynman
> ==================================
> "Computer science is the new mathematics."
>
> - Dr. Christos Papadimitriou
> ==================================
>
> _______________________________________________
> Edu-sig mailing list
> Edu-sig at python.org
> http://mail.python.org/mailman/listinfo/edu-sig
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20121223/284c0903/attachment-0001.html>


More information about the Edu-sig mailing list