This repository investigates numerical techniquues to approximate the value of π using both probabilistic (Monte Carlo) and deterministic (Leibniz and Nilakantha series) approaches. The project provides an educational and computational comparison of convergence behavior, precision, and implementation complexity of these classical algorithms.
- Monte Carlo Simulation (stochastic approach)
- Leibniz Series Expansion (alternating infinite series)
- Nilakantha Series Expansion (rapidly converging series)
The implementation is designed to provide both numerical and graphical insights into the convergence behavior and relative accuracy of each method.
The Monte Carlo method utilizes random sampling to approximate π. Points are uniformly distributed within a unit square. The ratio of points falling inside the inscribed unit circle provides a statistical estimate of π:
π ≈ 4 × (points inside circle) / (total points)
A well-known infinite series derived from the Taylor expansion of arctangent:
π ≈ 4 × (1 − 1/3 + 1/5 − 1/7 + 1/9 − ...)
While theoretically valid, its convergence is extremely slow.
An improvement over the Leibniz formula, this series converges more rapidly by adding and subtracting rational terms starting from 3:
π ≈ 3 + 4/(2·3·4) − 4/(4·5·6) + 4/(6·7·8) − ...
- Estimate π using numerical methods
- Measure absolute and relative error against the true value of π
- Visualize convergence trends across different algorithms
- Export results in machine-readable formats (CSV)
Method | π Estimate | Absolute Error | Relative Error (%) |
---|---|---|---|
Monte Carlo | 3.1416000000 | 0.0000073466 | 0.00023386 |
Leibniz | 3.1415926535 | 0.0000000001 | 0.00000000 |
Nilakantha | 3.1415926535 | 0.0000000001 | 0.00000000 |
The program generates a convergence plot showing:
- π estimates across iterations
- Method-specific trajectories
- Horizontal reference line representing true π
This allows for direct visual inspection of convergence speed and stability.
- Install dependencies:
pip install -r requirements.txt
- Run the script:
cd src
python main.py
This project is released under the MIT License.