[Edu-sig] generate digits of pi

david at handysoftware.com david at handysoftware.com
Sun Dec 23 00:25:50 CET 2012


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" <kurner at oreillyschool.com>
Sent: Saturday, December 22, 2012 5:33pm
To: 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/2befffb6/attachment.html>


More information about the Edu-sig mailing list