Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
dichotomy (half-division method) solution in Clear category for Super Root by kdim
def super_root(number):
a, b, d = 1, 10, 1
while not (-0.001 < d < 0.001):
x = (a + b) / 2
d = number - x ** x
(a, b) = (x, b) if d > 0 else (a, x)
return x
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"
March 3, 2021
Comments: