Share your repls and programming experiences

← Back to all posts
Approximating Pi with Coprimeness
meapandbob (0)

This program will approximate pi by generating 2 numbers (# of trials and max interval determined by the user) and determine if the 2 randomly generated numbers share any factors. If the 2 numbers share NO factors, they are considered coprime. Luckily, using a bit of calculus, you can determine that the proportion of coprime numbers to the total amount of trials is approximately equal to 6 / pi^2. You can then manipulate that to get

pi = sqrt(6 / ratioOfCoprimes)

The idea to create this program came from a video made by Stand-Up Maths: