Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Super Root by yarmak_vladislav
from decimal import *
def super_root(number):
getcontext().Emax = 10**10
left, right = Decimal(1), Decimal(10)
x = (right + left) / Decimal(2)
while True:
y = x**x
if abs(y - number) < 0.001:
break
if y < number:
left = x
x = (right + x) / 2
else:
right = x
x = (left + x) / 2
return float(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"
July 6, 2014
Comments: