[Edu-sig] generate digits of pi

Kirby Urner kurner at oreillyschool.com
Sun Dec 23 01:32:23 CET 2012


'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, <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" <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
>
> Kirby
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20121222/4a31a6b7/attachment.html>


More information about the Edu-sig mailing list