So did you download my Pi program? Or you just want to know some nerd algorithms? Well, read on.

How does one go about solving for pi? Well first of all let’s define some stuff to make things easier.

Let’s discuss what pi is first. Pi (every iteration of pi makes me mistype ‘pie’. Ah, typing while hungry.) is a number which relates the dimensions of a square to a circle, for instance a circle inscribed within a square. OK, non-geometry and non-math folks can close the window now.

A visual representation of the inscribed circle

Establishing that fact, and using the inscribed circle as a point of reference, we now enumerate some formulas related to the two shapes.

Given that a radius of a circle is equal to *r*, we can assume that the side length of the outer square is *2r. *Thus, the area of the circle can be represented as **πr**^{2}, and the area of the square as **4r**^{2}, from the definition that its side is *2r*.

Using what we have established so far, we can define a ratio, specifically the ratio between the area of the circle to the area of the square. Let’s define **A** as the area of the circle and **B** as the area of the square. With this, we can define such an equation:

**A/B** = **πr**^{2}/**4r**^{2}

Notice that we can actually cancel **r**^{2} from the equation, which leaves us with:

**A/B** = **π**/**4**

Rewriting and transposing some variables nets us with this:

**π** = **4 * (A/B)**

This then forms the basis of the algorithm used in the C++ program I wrote for solving Pi. The working algorithm can be seen in the source file included with the download. As you can see by studying the code, I have the algorithm coded right, but I can’t find the right values to use, hence approximation by random number generation and incrementing is implemented. In future versions I intend to rectify this.

So there you have it. That’s my approach to solving for pi. Comments, feedbacks and discussions on the topic are strongly encouraged, so comment away! 🙂

**Further Reading** (for the obsessed mathematicians)**:**

### Like this:

Like Loading...