from math import sqrt
def arithmetic_geometric_mean(a, g):
while True:
a= (a+g)/2
g=sqrt(a*g)
yield a, g
x = float(input("x="))
a_0 = (1+x)/2
g_0 = sqrt(x)
tol= 1e-5
def approx_ln(x, maxit=500):
a_0= (1+x)/2
g_0=sqrt(x)
for i in range(maxit):
for a,b in arithmetic_geometric_mean(a_0, g_0):
if abs(a-b)<tol:
return (x-1)/a
return (x-1)/a
print(approx_ln(x,10000))
Comments
Leave a comment