-
Notifications
You must be signed in to change notification settings - Fork 1
/
calculate_pi.py
38 lines (26 loc) · 919 Bytes
/
calculate_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
37
38
import time
import sys
import numpy as np
# Here we calculate pi by assessing the area under the curve, using the CPU
# Then we check the value against what is calculated in numpy
# source: https://github.com/UNM-CARC/QuickBytes/blob/master/workshop_slides/CS_Math_471.pdf
def Pi(num_steps):
step = 1.0 / num_steps
sum = 0
for i in range(num_steps):
x = (i+0.5) * step
sum = sum + 4.0 / (1.0 + x * x)
pi = step * sum
return pi
if len(sys.argv)!=2:
print("Usage: ", sys.argv[0], "<number of steps>")
sys.exit(1)
num_steps = int(sys.argv[1],10)
# Call the function to calculate pi
start = time.time() #Start
pi = Pi(num_steps)
print(type(pi))
end = time.time() # End
# Print estimation, the difference from the numpy val and how long it took.
print("Pi = %.20f, (Diff=%.20f) (calculated in %f secs with %d steps)" %(pi, pi-np.pi, end-start, num_steps))
sys.exit(0)