-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecimal_digit_pi.py
More file actions
50 lines (36 loc) · 1.07 KB
/
decimal_digit_pi.py
File metadata and controls
50 lines (36 loc) · 1.07 KB
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
from special_number_series import bernoulli
def basic_factorial(x):
"""Returns the factorial of the integer x."""
ans = 1
while x:
ans *= x
x -= 1
return ans
def helper_pi(n: int) -> float:
m = 2*n
num = 2 * basic_factorial(m)
a = (1 - pow(2, -m)) * (1 - pow(3, -m)) * (1 - pow(5, -m)) * (1 - pow(7, -m))
den = abs(float(bernoulli(m))) * a
return pow(num / den, 1 / m) / 2
def decimal_digit_extraction(n: int) -> int:
"""Returns the nth digit of pi"""
x = helper_pi(n-1) * pow(10, n-1)
k = int(10 * (x - int(x)))
return k
def f(k):
"""Bailey-Borwin-Plouffe formula"""
k8 = 8*k
result = 4/(k8 + 1) - 2/(k8+4) - 1/(k8+5) - 1/(k8+6)
return result / pow(16, k)
def bbp_series(max_n=11):
pi = 3.141592653589793
ans = 0
# testing convergence just for fun
for n in range(max_n):
ans += f(n)
print(ans, pi, ans - pi)
if __name__ == '__main__':
# bbp_series(max_n=11)
pi = 3.141592653589793
for i in range(3, 10):
print(str(pi)[i+1], decimal_digit_extraction(i))