-
Notifications
You must be signed in to change notification settings - Fork 0
/
#14.py
38 lines (28 loc) · 890 Bytes
/
#14.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
37
38
"""
This problem was asked by Google.
The area of a circle is defined as πr^2. Estimate π to 3 decimal places using a Monte Carlo method.
Hint: The basic equation of a circle is x2 + y2 = r2
"""
# had to google what a Monte Carlo method is: essentially one creates random points .
import random
from math import sqrt
# if r == 1, the area becomes pi
import numpy
def pi_estimator(number_of_digits):
number_inside = 0
number_outside = 0
for i in range(0, number_of_digits):
x = numpy.random.rand()
y = numpy.random.rand()
if is_inside(x, y):
number_inside += 1
else:
number_outside += 1
return 4 * number_inside / (number_outside + number_inside)
def is_inside(x, y):
if x * x + y * y < 1.0:
return True
else:
return False
if __name__ == '__main__':
print(pi_estimator(9000000))