def product(n,term,next,initial):
total,k = initial,0
while k<=n:
total,k = total*term(k),next(k)
return total
def pi_next(k):
return k+2
def pi_term(k):
return ((k+4)/(k+3))**2
def pi_product(n=10**6):
return product(n,pi_term,pi_next,8/(n+4))
print(pi_product())
def square_root(root):
return find_root(lambda x:x*x-root)
def logarithm(a,base = 2):
return find_root(lambda x:base**x - a)
def find_root(f,initial_guess = 10):
def test(x):
return approx_eq(f(x),0)
return iter_improve(newton_update(f), test, initial_guess)
def iter_improve(update, test, guess=1):
while not test(guess):
guess = update(guess)
return guess
def newton_update(f):
def update(x):
return x - f(x)/approx_derivative(f,x)
return update
ef approx_derivative(f,x,delta = 1e-5):
df = f(x+delta) - f(x)
return df/delta
def approx_eq(a,b):
if not a or b:return True
return abs(a-b)<1e-5 and ( abs((a-b)/b) <1e-5 if b else abs((a-b)/a -1)<1e-5)
def cont_frac(term_n,term_d,k):#compute the infinite fraction
i,total = k,0
while i!=0:
total,i = term_n(i)/(total+term_d(i)),i-1
return total
print(cont_frac(lambda i:1.0,lambda i:1.0,100))
def rec_confrac(term_n,term_d,k):
def contfrac(i):
if i==k:return 0
return term_n(i)/(term_d(i)+contfrac(i+1))
return contfrac(0)
def compute_e(k):
def term_d(i):
if (i-2)%3:return 1
else:return 2*((i-2)//3+1)
return 2+cont_frac(lambda i:1,term_d,k)
print(compute_e(100))
def cubic(a,b,c):
return find_root(lambda x:x**3+a*x**2+b*x+c)
print(cubic(0,-300,0))
def repeat(f,n):
def wrap(x):
i = 1
while i<=n:
i,x = i+1,f(x)
return x
return wrap
def double(f):
return repeat(f,n=2)
t = double(lambda x:x*5)
执行double(double(double))(lambdax:x+1)(5) 得到的结果是21。它的规律是每次都是上一次调用次数的平方。
def abstract_smooth(f,average):
def wrapper(x):
dx = 1e-5
return average(f,x,dx)
return wrapper
def average3(f,x,dx):
return (f(x)+f(x-dx)+f(x+dx))/3
def smooth(f):
return abstract_smooth(f,average3)
def mul_smooth(f,n=3):
return repeat(smooth,n)(f)
t = mul_smooth(lambda x:x**6 if x>=10 else 100)