-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpi_python.py
More file actions
24 lines (23 loc) · 824 Bytes
/
pi_python.py
File metadata and controls
24 lines (23 loc) · 824 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
import mpmath
def pi_nth_digit(n: int) -> int:
def pi_n(n):
B2n = -2*n * mpmath.zeta(1 - 2*n)
nominator = 2 * (-1)**(n+1) * mpmath.factorial(2 * n)
denominator = 2**(2 * n) * B2n * (1 - 2**(-2 * n)) * \
(1 - 3**(-2 * n)) * (1 - 5**(-2 * n)) * \
(1 - 7**(-2 * n) * (1 - 11**(-2 * n)))
return (nominator / denominator) ** (1 / (2 * n))
if n < 0:
raise ValueError("n must be a non-negative integer")
elif n == 0:
return 3
elif n == 1:
return 1
elif n == 2:
return 4
else:
pi_n_minus_1 = pi_n(n-1)
# Extract fractional part from pi_n_minus_1
frac_part = mpmath.fmod(pi_n_minus_1 * mpmath.power(10, n - 1), 1)
dn = int(mpmath.floor(10 * frac_part))
return dn