# [Tutor] concerning Monte Python method you suggested for pi, also PiPoem I found

Charles Gruschow, Jr. c.gruschow@prodigy.net
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)
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.
Thanks,
c.gruschow@prodigy.net

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
3.141592654
9991 3.1172                 count
100*(ave/pi)
9992 3.1128                 10000
100.0008374
9993 3.158                         max
ave-pi
9994 3.144                         3.2084
2.63064E-05
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
0.016459693
variance population
0.000270921
ave. absolute deviations
0.013152446

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/)

[

#!/usr/local/bin/python
# 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!!!)

import sys

def main():
k, a, b, a1, b1 = 2L, 4L, 1L, 12L, 4L
while 1:
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:
output(d)
a, a1 = 10L*(a%b), 10L*(a1%b1)
d, d1 = a/b, a1/b1

def output(d):
sys.stdout.write(`int(d)`)
sys.stdout.flush()

main()

# Reading/writing Python source often gives me the impression of
# 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. <c.gruschow@prodigy.net>
Cc: <tutor@python.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)
> 3.6
> >>> getPi(100)
> 3.32
> >>> getPi(1000)
> 3.144
> >>> getPi(10000)
> 3.1132
> >>> getPi(100000)
> 3.14792
> >>> getPi(1000000)
> # After a long long long time...
> 3.141808
> ###
>
> So this isn't quite an efficient way of calculating Pi, but it does work.
>

```