Jump to content

How can I calculate Pi?


goldenpeach

Recommended Posts

Bah. It's always best to start with something simple that you can really understand. The most basic way to compute Pi is a geometric one. Start with a regular hexagon inscribed in a circle of radius 1. Its perimeter is 6, which gives you first approximation Pi=3. Then drop a perpendicular bisector on each side to construct a regular dodecagon inscribed in the same circle. You should be able to use Pythagoras' Theorem to compute the new perimeter. Keep going. As the polygon gets rounder and rounder, your approximation will get closer to the value of Pi.

The bad news is that it converges ridiculously slowly, so it's not something you'd use for thousands of digits, but it's a good start to see how the algorithm works.

Then you can sit down and try to understand more advanced formulae and where they come from. Most of them are highly non-trivial, and programming them is much easier than understanding.

Link to comment
Share on other sites

There an even simpler way.

Build a large square matrix, of size r*r.

Now count how many points of this matrix are less than r away from the origin, you can do that by checking if x²+y²<=r².

The points that fit that rule are within a quarter disk of area pi*r²/4, and the whole matrix has an area r².

So take the number of points in the quarter circle, multiply by 4, divide by the number of points in the whole matrix, and you get an approximation of pi.

the more points in the matrix, the more accurate the result.

You can optimize by:

doing that only for one half of the matrix (it's symmetric along the diagonal)

measuring the surface of a ring with r1 > distance >r2. If you do it smartly, you will only have to check a few points (all points in the ring have neighbours that are also in the ring)

massively parallelisation of the task.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...