Monte Carlo Pi Estimate

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]

 

 

Search

Search WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.

 

Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. 

     

We receive a few cents from each purchase.  Thanks

 


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa® Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa® 365 Puzzlers  Calendar 2017

Mensa® 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)

Contact

Feedback:  Send an e-mail with your comments about this program (or anything else).

Search DelphiForFun.org only

 

 

 

 

Problem Description

The other day someone told me that the probability that any randomly chosen pairs of integers are relatively prime is an interesting number.

"In fact," she said, " if we divide that probability into 6 and take the square root, the result is Pi!" 

This program  checks and see if that could be true.

Numbers are relatively prime if they have no common factors greater than 1. Pi is the ratio of the circumference of a circle to its diameter.

Background & Techniques

I can't remember where I ran across this hypothesis.   Nor have I been able to find the math, or even  a rationale as to why it might be true.   But it does appear to be. 

We'll get a number of random pairs to check from the user and then loop that many times counting the number that are relatively prime.  At the end of the loop, the Pi estimate is calculated  as the SQRT(6*NbrPairs / NbrPairsRelativelyPrime)

Saying that two numbers are relatively prime is the same as saying that their greatest common divisor is 1.  We'll use a simple GCD function to check for relative primeness.    For more info you can check  this GCD Math Topic

The usual features of my programs with potentially long  loops are here:

  1. A STOP button to end the loop.   Positioned over the Start button and defined initially as invisible.
  2. Before starting of the loop, make the STOP button visible, set Form1.Tag:=0 and  Screen.Cursor:=crHourGlass.
  3. When the STOP button is pressed, set Tag:=1;
  4. Inside the loop, check for Tag<>0 periodically (every 1024 passes in this program) and break out if true.
  5. After the loop ends make he STOP button invisible again and set the cursor back to crDefault.

And in case you want to try several million samples, results are accumulated and the cumulative  estimate also displayed with each run.

Running/Exploring the Program 

bulletBrowse source extract
bulletDownload source
bulletDownload  executable

Suggestions for Further Explorations

Check out series summing methods which give much better estimates much faster than random sampling methods.   The best of the known algorithms for calculating Pi is the Borwein quartically convergent algorithm and has been used to calculate the first 200 billion digits of Pi.   Each iteration adds several hundred digits of accuracy.

The whole "Pi digits" exercise may be mathematically pleasing but probably has little real world application.  The first 39 digits of PI were  calculated  350 years ago.  That is accurate enough to calculate the circumference of a circle 40 billion light years in diameter with a error of less than the width of one hydrogen atom!  I'd say that 39 digits is sufficient for all practical purposes.   

  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright © 2000-2018, Gary Darby    All rights reserved.