An open API service indexing awesome lists of open source software.

https://dvjn.github.io/CalcuPi/

Calculate Pi using Monte Carlo method
https://dvjn.github.io/CalcuPi/

Last synced: 8 months ago
JSON representation

Calculate Pi using Monte Carlo method

Awesome Lists containing this project

README

          

# **[`CalcuPi 🚀`](https://dvjn.github.io/CalcuPi/)**

> Calculate Pi using Monte Carlo method

Please ⭐ if you like it!

## Explanation 🤔

### Steps 🎲

- Use a square of arbitrary side length and inscribe a circle inside it.
- Put random points on the square using a uniform distribution.
- Count the number of random points falling inside the circle.
- Calculate the approximate value of pi using,
`4 * points inside the circle / total number of points`.

### Maths 📏

Why 4 * points inside the circle / total number of points?


Let's consider,


Radius of circle, **`r`**


Side of square, **`2r`**


Number of points inside the circle, **`C`**


Total number of points, **`S`**

Thus,


Area of circle = **`πr²`**


Area of square = **`4r²`**

For large enough number of random points, we can consider that the ratio of areas of circle to square is equal to the ratio of points inside the circle to the total number of points, ie, **`πr²/4r² = C/S`**.


Which can be simplified to, **`π = 4*C/S`**

## Links ✨

- [What is Monte Carlo Method? 🤓](https://en.wikipedia.org/wiki/Monte_Carlo_method "Monte Carlo method - Wikipedia")
- [Inspiration for the Project 💖](https://www.youtube.com/watch?v=5cNnf_7e92Q "Coding Challenge #95: Approximating the Value of Pi - Youtube")