You can use the previous numerator to compute a numerator. Using this, you can do:
from math import sqrt
def pi_approx(n):
V = 1
numerator = 0
for _ in range(n):
numerator = sqrt(2 + numerator)
V *= numerator / 2
return 2 / V
print(pi_approx(1000000)) # 3.141592653589794
But as trincot points out, note that the result is governed by float point precision; i.e., difference between the output and math.pi
is not quite meaningful.
1
solved How can I use the method init in this function [closed]