# [Edu-sig] generate digits of pi

michel paul pythonic.math at gmail.com
Mon Dec 24 00:49:52 CET 2012

```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
> >>> 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."