Pi is a very illusive number that has puzzled and amazed mathematicians and scientists alike for generations. Being an irrational number we can never find out what it is exactly, so how do we use it. There are a number of ways to estimate pi by measuring the circumference of a circle and then dividing by the diameter, but that would require near perfect measurements and a nearly perfect circle to get a good estimate. So, how do you find pi? Fortunately, using computer programming, pi can be estimated with a relatively simple program. To access my programs, see the text documents on the last page of this instructable.
Step 1: Understanding the concept
In order to write a program you must first understand what you are doing. For this program, we are finding pi by comparing the area of a circle and a square. Imagine a circle with a radius of one inscribed inside a square. It would look a lot like this image. So, using the formulas a=b*h and a=pi*r^2, finding the area of the square is easy, 2*2=4, and finding the area of the circle, in terms of pi, is just as simple, 1*1*pi=pi. The ratio of the two areas is pi to 4, or pi/4 to 1, so if we can approximate this ratio we can determine pi. The question is, how do we do this? Using a monte carlo method, we can estimate this ratio by randomly finding points inside the square and determining if it is inside the circle, which will give us an approximate area. The more trials that are run, the more accurate the number will be. Now, letís put it into code.
Step 2: Starting the code
Depending on which language and compiler you are using, you will have to begin the code differently. I have provided examples for java and c++ in this project. I used the codeblocks and bluejay compiler.
Step 3: Declaring the variables
In order to complete this program, you will need variables for the number of trials, the number of trials found to be in the circle, and the pi estimate. I included other variables to represent x and y coordinates so as to make it more easily understandable. The variable for number in the circle must be set equal to zero, all other will be dealt with later.
Step 4: Determining number of trials
As stated earlier, the more trials that are run, the more accurate the number. In order to allow the user to decide how accurate they want it, they can decide the number of trials.
Step 5: Running each trial
A loop must be created to go through each and every trial.
Step 6: Running a trial
In order for this program to work, you need a random number generator. Using this, you randomly select number on a range of zero to one for x and y coordinates. This will give you points in quadrant one, but since both shapes are symmetrical about both axis, other quadrants are not needed. Using the Pythagorean Theorem, you can determine it the distance to that point is greater than or less than the radius of the circle. If it is less than or equal to, it is part of the inscribed circle and the count for number in the circle should be increased by one. This must all be done inside the loop.
Step 7: Interpreting the data
The number of trials is a representation of the area of the square, since all the points were in the square, and the number in the circle is representative of the area of the circle. In order to determine pi we must first simplify the ratio. Currently, the ration is the number in the circle to the total number. By dividing by the total number we get the number in the circle divided by the total number to one. Referencing back to our calculated ratios, we find that the ratio is pi/4 to one, so the number in the circle divided by the total number is equal to pi/4. By multiplying both sides by 4 we find that pi = 4*number in circle/total number.
Step 8: Displaying the results
This step is self-explanatory.
Step 9: Try it out!
The program is finished and now you can test it out. Be cautious when choosing large numbers of trials because it may take some time. I have included my code in both c++ and java, as well as a program that can conduct more trials, however takes much longer.