-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonte.py
More file actions
34 lines (28 loc) · 1000 Bytes
/
Copy pathmonte.py
File metadata and controls
34 lines (28 loc) · 1000 Bytes
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
import math
import random
# from time import perf_counter
# burayı alttaki satırla değiştirdim
from timeit import default_timer as perf_counter
def monte_carlo(start, stop, num_points, f):
hits = 0
upp_bound = 0
for i in range(num_points):
x = random.uniform(start, stop)
y = random.uniform(0, 4) # this is cheating since I already know it's 4
if y <= f(x):
hits += 1
if y > upp_bound:
upp_bound = y
ans = (hits / num_points) * ((stop - start) * upp_bound)
return ans
def riemann(start, stop, step, f):
return sum(f(start+step*m)*step for m in range(int((stop - start)/step)))
f = lambda x: math.sqrt(16 - 16*x**2)
start = perf_counter()
a = monte_carlo(0, 1, 10000000, f)
m_time = perf_counter() - start
start = perf_counter()
b = riemann(0, 1, 1/15000, f)
r_time = perf_counter() - start
print('Monte Carlo: %s\nTime elapsed: %s' % (a, m_time))
print('Riemann sum: %s\nTime elapsed: %s' % (b, r_time))