-
Notifications
You must be signed in to change notification settings - Fork 0
/
PyPi.py
46 lines (40 loc) · 1.11 KB
/
PyPi.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
import time
import numpy as np
#Calculate pi to a given number
def Leibniz(iterations):
n = 1
for i in range(2,iterations - 1):
term = ((-1.0) ** (i - 1.0)) / ((2 * i) - 1.0)
n = n + term
return n*4
def LeibnizNP(iterations):
i = np.arange(0, iterations)
term = ((-1.0) ** i) / ((2 * i) + 1.0)
n = np.sum(term)
return n*4
def LeibnizTurbo(iterations):
n = 1
topterm = -1.0
for i in range(2,iterations - 1):
bottomterm = i << 1 #Fast bitwise x2 whilst i still int
term = topterm / (bottomterm - 1.0)
n = n + term
topterm = -topterm #Flip top between 1 and -1 without exponentiating
return n*4
def LeibnizTurboNP(iterations):
topterm = np.ones(iterations)
topterm[1::2] = -1
bottomterms = np.arange(0, iterations*2, 2) + 1
terms = topterm / bottomterms
n = np.sum(terms)
return n*4
#Main
print()
print("Starting Leibniz Approximation")
start_time = time.time()
print(LeibnizTurboNP(100000000))
end_time = time.time()
elapsed = end_time - start_time
print(f"Elapsed Time: {elapsed:.4f} seconds")
print()
print()