Share your repls and programming experiences

← Back to all posts
##### Pi Challenge

I succeeded write the pi challenge in python, write a function that by given an integer n - calculate the accuracy of pi

##### Comments
hotnewtop
malvoliothegood (845)

What is the theory behind the algorithm?

kggold4 (0)

@malvoliothegood
the idea of this algorithm:
imagine you have a circle with radius of 1,
now, by randomly choosing n points in the right top side of the circle (only positive) you want to get the number of points that are in the circle and the number of point that are out of the circle,
how do we do this? because the radius of the circle is 1, the area of the circle is pi (because the area formula is pi*r^2 (r=1)), so if we are relating only to the positive right top side, this are of the circle is pi/4, by randomly choose n integers we count the numbers of k points that are in the circle by using the distance formula with the middle point of the circle (0,0), and after that we calculate k/n to get the ratio and multiply by 4 to get pi, and the more that n is big the more we get pi in more accuracy

malvoliothegood (845)

@kggold4 Excellent explanation thanks. What a clever algorithm. The only issue is that it takes some time to run.