[Tutor] concerning Monte Python method you suggested for pi, also PiPoem I found
Charles Gruschow, Jr.
Tue, 29 Aug 2000 04:15:16 -0500
I did a little statistics on that program.
10000 loops of 10000 "arrows" (used getPi(10000) 10000 times)
It took about 4 hours.
One ?--->why are all our ratios coming out to 3 or 4 digits, is it because
we are dividing by 10000?
This method doesn't seem too accurate or quick, does it.
P.S. do do these statistics I pasted output into a text file and then opened
the text file in Excel,I had Excel interpret spaces as the field breaks.
9989 3.1508 average(mean) pi
9990 3.1372 3.14161896
9991 3.1172 count
9992 3.1128 10000
9993 3.158 max
9994 3.144 3.2084
9995 3.1584 min
9996 3.1452 3.0744
9997 3.1476 median
9998 3.1376 3.142
9999 3.164 mode
10000 3.1416 #NUM!
standard deviation population
ave. absolute deviations
P.P.S. I found following off Ivan Van Laningham's website for his book
"Teach Yourself Python in 24 hours"(http://www.pauahtun.org/TYPython/)
# Contribution to the forever lasting discussion on indentation!
# After Petrarca, Shakespeare, Milton, Drs. P and many, many others,
# a sonnet has 14 lines and a certain rhyme scheme.
# Jacques Bens presented in 1965 in Paris the pi-sonnet with
# 3,1,4,1 and 5 lines, but the ultimate pi-poem I found in
# Brown's Python Annotated Archives p. 12:
# Based on a algorithm of Lambert Meertens (remember those days of the
# B -> ABC-programming language!!!)
k, a, b, a1, b1 = 2L, 4L, 1L, 12L, 4L
p, q, k = k*k, 2L*k+1L, k+1L
a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
d, d1 = a/b, a1/b1
while d == d1:
a, a1 = 10L*(a%b), 10L*(a1%b1)
d, d1 = a/b, a1/b1
# Reading/writing Python source often gives me the impression of
# reading/writing a poem!
# Layout, indentation, rythm, I like the look and feel!
# What does this tiny program do? It is not a sonnet, even not a
# pi-sonnet, but it surely produces Pi!
# The poem ( sorry, the program) needs some explanation.
# As a mathematician I recognize the continued fraction, odd/even,
# squares and all that matters.
# But it is a miracle! A few lines of Python code producing
# a infinity of pi-digits!
# Jaap Spies
# Hogeschool Drenthe
# Keep Peace in Mind
----- Original Message -----
From: Daniel Yoo <dyoo@hkn.EECS.Berkeley.EDU>
To: Charles Gruschow, Jr. <firstname.lastname@example.org>
Sent: Monday, August 28, 2000 1:10 AM
Subject: Re: [Tutor] re: Monte Python method you suggested for pi
> > the last message I sent, I meant Monte Carlo not Monte Python,
> > oops... :)
> Actually, that name is oddly appropriate... *grin*
> There's a slight bug in the program --- it has to do with the way the
> tuple assignment works. The line:
> > rsqr,r=xt**2+yt**2,sqrt(rsqr)
> will compute the two values on the right hand side first, and _then_ do
> the tuple assignments afterwards. The problem is that at the time Python
> computes the right hand side, 'rsqr' isn't known. To fix this, you'll
> have to do it the non-tricky way to have the same effect.
> The rest of the program looks good! I couldn't help but try my own hand
> at this. I found a way of simplifying the logic --- just compare the
> ratio between the circle and the whole area. That is,
> Pi / 4 = (# of points in circle) / (total points)
> which can be restated as:
> Pi = 4 * (# of points in circle) / (total points)
> Here's the program that implements this:
> ### pi.py (pie pie!)
> from whrandom import random
> from math import sqrt
> def makePoint():
> x = (random() * 2) - 1
> y = (random() * 2) - 1
> return (x,y)
> def insideCircle(p):
> x, y = p
> return sqrt(x**2 + y**2) < 1
> def getPi(iterations):
> count = 0
> for i in xrange(iterations):
> if insideCircle(makePoint()):
> count = count + 1
> return 4 * count / float(iterations)
> And a few sample runs:
> >>> getPi(10)
> >>> getPi(100)
> >>> getPi(1000)
> >>> getPi(10000)
> >>> getPi(100000)
> >>> getPi(1000000)
> # After a long long long time...
> So this isn't quite an efficient way of calculating Pi, but it does work.