Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # cbrt(x) = n such that n^3 = x
- # n^3 = x
- # n^3 - x = 0
- # f(n) = n^3 - x
- # f'(n) = 3n^2
- # g(n) = n - (f(n)/f'(n))
- # cbrt(x) = (approx) g^10(1) where x = x in all above functions
- # we can generalize this to other 1/x exponents
- def root(e, x):
- f = lambda g: g**e - float(x)
- fp = lambda g: e * g**(e-1)
- g = lambda g: g - f(g) / fp(g)
- guess = 1.0
- for i in range(10):
- guess = g(guess)
- return guess
- # example usage: root(4, 3) calculates the 4th root of 3 (n where n*n*n*n = 3)
- # ~1.316074012952492
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement