### 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.

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

### Contact

 Search DelphiForFun.org only

### The Problem

There should be some long story here about how and why the king ordered that lots of cannonballs be fired into a field just containing a circular pond.  But I'm a programmer, not a storyteller - so the essence is this: Assign points randomly inside a square area that just contains a circular area, and  the ratio of hits in the circle to total shots.  It must be related somehow to Pi.  [Pi, by definition is the ratio of the distance around a circle  (circumference) to the distance across (diameter).]

### Background & Techniques

If we assume that the square is 1 mile on a side and the pond is 1 mile across, then the area of the square is 1 square mile.  The radius of the pond is  1/2 mile and its area is Pi*R2 or  0.25Pi.  If the shots are uniformly distributed over the area of the field then the ratio of shots in the pond to shots in the field is approximately the same as the ratio of the two areas (.25Pi/1 or just Pi/4).  So the best estimate of Pi is simply 4 times this ratio.

The plan is to generate random numbers inside the field, and increment the inpond count if it is within 1/2 mile of the center of the pond. We can use the Pythagorean theorem to calculate the distance of our random points from the center of the field.

Computationally, we use the randomize function to start generating random numbers with different random values for each run.  Without randomize, the numbers generated will be random (actually called pseudo-random), but would be repeatable from run to run.  The random function returns a random number between 0 and 1 if called without parameters.  If we assume a coordinate system with origin at the center the field, we can subtract 1/2 from the random points and end up up x and y values between -1/2 and +1/2.  By the Pythagorean theorem for  distance, x2+y2=d2, so the point is in the pond if x2+y2<.25.

That's about it.  I initialized the two count fields using the constant declaration feature of Delphi, added a reset button to reset the counters at any time.  This allows multiple clicks on the Shoot! button to produce cumulative results - we should be able to see the estimates get close to Pi as the number of shots increases.

### Suggestions for Further Study

It seems like graphics would be nice to show the cannonballs, the problem is that after a few thousand shots, the pond and the field are covered with canon balls and not much is seen (also graphics would slow the program down by a lot).   So, having tried it,  I can't recommend graphics for this app.

Pi is an interesting number with lots of literature on the Web about it.  There are other techniques that converge to the true value of Pi much faster than the one presented here.   Another program in  this set, uses Buffon's Needles technique to estimate Pi.   Many series converge to some value related to Pi, for example pi /4 = 1 - 1/3 + 1/5 - 1/7 + .... or  pi/2 = 2/1 x 2/3 x 4/3 x 4/5 x 6/5 x 6/7 x 8/7 x 8/9 ...   I wonder which series converges fastest?   Or if that is even known?