Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
log then Halley solution in Speedy category for Super Root by Sim0000
from math import log
def super_root(number):
a = log(number)
x = a # initial value
while abs(x**x - number) >= 0.001:
f0, f1, f2 = x*log(x) - a, log(x) + 1, 1 / x
x = x - 2*f0*f1 / (2*f1**2 - f0*f2)
return x
# Solve x*log(x) = a using Halley's iteration
# f(x) = x*log(x) - a
# f'(x) = log(x) + 1
# f''(x) = 1/x
# This code is faster then previous Newton version.
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
def check_result(function, number):
result = function(number)
if not isinstance(result, (int, float)):
print("The result should be a float or an integer.")
return False
p = result ** result
if number - 0.001 < p < number + 0.001:
return True
return False
assert check_result(super_root, 4), "Square"
assert check_result(super_root, 9), "Cube"
assert check_result(super_root, 81), "Eighty one"
July 4, 2014
Comments: