-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC06P04.py
51 lines (31 loc) · 805 Bytes
/
C06P04.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
39
40
41
42
43
44
45
46
47
48
49
50
51
# Solution
import random as r
import math as m
def getPoint():
x = r.random() ** 2
y = r.random() ** 2
return (x, y)
def isPointInside(point, radius):
if m.sqrt(point[0] + point[1]) < radius:
return True
return False
def calculatePi(inside, total):
# inside / total = pi / 4
return (float(inside) / total) * 4
def problem(iterations):
radius = 1.0
counter = 0
for i in range(0, iterations):
point = getPoint()
if isPointInside(point, radius):
counter += 1
numericalResult = calculatePi(counter, iterations)
return numericalResult
# Asserts
print(getPoint())
iterations = int(input("Number of iterations?"))
print(iterations)
print(m.pi)
result = problem(iterations)
print(result)
print(m.pi - result)