Or just replace

        while d == d1:
with
        while d == d1 and n > 0:

Gary Litvin
www.skylit.com

At 11:54 PM 12/23/2012, da...@handysoftware.com wrote:

Here's an easier, pythonic way to limit the number of digits, given the original, non-terminating pi generator:

>>> import itertools
>>> print(list( itertools.islice(pi_digits(), 10) ))
[3, 1, 4, 1, 5, 9, 2, 6, 5, 3]



David H



-----Original Message-----
From: "michel paul" <pythonic.m...@gmail.com>
Sent: Sunday, December 23, 2012 6:49pm
To: "Kirby Urner" <kur...@oreillyschool.com>
Cc: da...@handysoftware.com, "edu-sig@python.org" <edu-sig@python.org>, "michel paul" <pythonic.m...@gmail.com>
Subject: Re: [Edu-sig] generate digits of pi

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 <<mailto:kur...@oreillyschool.com>kur...@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, <<mailto:da...@handysoftware.com>da...@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
<mailto:Edu-sig@python.org>Edu-sig@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@python.org
http://mail.python.org/mailman/listinfo/edu-sig
_______________________________________________
Edu-sig mailing list
Edu-sig@python.org
http://mail.python.org/mailman/listinfo/edu-sig

Reply via email to