import numpy as np
def frac_one(n: int, i: int = 0):
+= 1
i -= 1
n = 1 + 2 * i
nominator if n == 0:
return 1
else:
return 2 + (1 + 2 * i) ** 2 / frac_one(n, i)
def frac_inf(n: int, i: int = 0):
+= 1
i -= 1
n = 1 + 2 * i
nominator if n == 0:
return np.inf
else:
return 2 + (1 + 2 * i) ** 2 / frac_inf(n, i)
\(\pi = \frac{4}{1+\frac{1^2}{2 + recursion(n)}}\)
def pi_approx(func, n_iters):
return 4 / (1 + 1 / func(n_iters))
def pi_mixed(n_iters):
= pi_approx(frac_one, n_iters)
a = pi_approx(frac_inf, n_iters)
b return (a + b) / 2
# let's use our laptop's recursion limit to get maximum accuracy
import sys
= int(sys.getrecursionlimit() * 0.98)
max_recursion max_recursion
2940
pi_approx(frac_one, max_recursion)
3.141932789654053
pi_approx(frac_inf, max_recursion)
3.141252517545209
print(
f"""
At recursion depth of {max_recursion} the approximation error is:
* {(np.pi - pi_approx(frac_one, max_recursion))/np.pi:.3%} for frac_one
* {(np.pi - pi_approx(frac_inf, max_recursion))/np.pi:.3%} for frac_inf
* {(np.pi - pi_mixed(max_recursion))/np.pi:.12%} for pi_mixed
"""
)
At recursion depth of 2940 the approximation error is:
* -0.011% for frac_one
* 0.011% for frac_inf
* -0.000000000313% for pi_mixed