forked from devAmoghS/Python-Interview-Problems-for-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
estimate_pi.py
37 lines (27 loc) · 922 Bytes
/
estimate_pi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import numpy as np
from math import pi as PI
def estimate_pi(sims):
"""
takes the number of simulations as input to estimate pi
"""
# counter to hold points lying inside the circle
in_circle = 0
for s in range(0,sims):
x = np.random.rand()
y = np.random.rand()
if (x**2 + y**2) <= 1:
in_circle += 1
# The ratio of pts. inside the circle and the total pts. will be same as the ratio
# of the area of circle to the area of the square, inside which the circle is inscribed
# Area of circle = PI * R * R
# Area of square = (2R) * (2R)
pi_estimated = 4.0 * in_circle / sims
print("Simulations ran: ", sims)
print("Estimated pi", pi_estimated)
print("Error", PI - pi_estimated)
pow = 0
input_sims = 100
while pow <= 8:
estimate_pi(sims=input_sims)
pow += 1
input_sims *= 10