[Edu-sig] generate digits of pi

david at handysoftware.com david at handysoftware.com
Sun Dec 23 01:40:18 CET 2012


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
 
-----Original Message-----
From: "Kirby Urner" <kurner at oreillyschool.com>
Sent: Saturday, December 22, 2012 7:32pm
To: david at handysoftware.com
Cc: edu-sig at python.org
Subject: Re: [Edu-sig] generate digits of pi



'3141592653589793238462643383279502884197169399375105820974944592307816406286208'

Yeah, I see (that was using 3.2).  Different frazzle at the end of the rope, but if you ask for more digits, they continue to agree out to the last bit and so on.  Is that it?

Kirby



On Sat, Dec 22, 2012 at 3:25 PM,  <[mailto:david at handysoftware.com] david at handysoftware.com> wrote:

Thanks for posting this.
 
I had to try it out. I found it behaves differently depending on the version of Python you use.
 
Python 3.2.2: pi_digits(79) generates 79 digits:
 
3141592653589793238462643383279502884197169399375105820974944592307816406286208
 
Python 2.6.5: pi_digits(79) generates 81 digits:
 
314159265358979323846264338327950288419716939937510582097494459230781640628620899
 
The 80th and 81st digits generated by Python 2.6.5 are correct, but unasked for. Assuming that the difference in behavior was due to the difference in the behavior of the division operator (what else could it be?) I ran it again using "python -Qnew" and this time got 82 digits:
 
3141592653589793238462643383279502884197169399375105820974944592307816406286208998
 
Bizarre. Tricky. On which version of Python was this generator intended to run, I wonder?
 
David H


 
-----Original Message-----
From: "Kirby Urner" <[mailto:kurner at oreillyschool.com] kurner at oreillyschool.com>
Sent: Saturday, December 22, 2012 5:33pm
 To: [mailto:edu-sig at python.org] edu-sig at python.org
Subject: [Edu-sig] generate digits of pi



I'm taking the liberty of reposting this generator supplied by Pythonista michel paul on Math Future.  He's not the author though.





def pi_digits(n):



k, a, b, a1, b1 = 2, 4, 1, 12, 4



while n>0:



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)



n -= 1



a, a1 = 10*(a%b), 10*(a1%b1)



d, d1 = a/b, a1/b1

More context:

[https://groups.google.com/d/msg/mathfuture/LA0pMPC6-HE/MBGWxn4ENsUJ] https://groups.google.com/d/msg/mathfuture/LA0pMPC6-HE/MBGWxn4ENsUJ

Kirby

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20121222/a921f55f/attachment-0001.html>


More information about the Edu-sig mailing list