Skip to content

Instantly share code, notes, and snippets.

@kirarpit
Created August 10, 2020 05:44
Show Gist options
  • Save kirarpit/c8b5c892801432cd1278d6049a5607ca to your computer and use it in GitHub Desktop.
Save kirarpit/c8b5c892801432cd1278d6049a5607ca to your computer and use it in GitHub Desktop.
def root(x, n):
if n == 1:
return x
if x == 1:
return x
high = x if x > 1 else 1
low = 0
mid = (high+low)/2
while (high - low) / 2 >= 1e-4:
if mid ** n == x:
return mid
elif mid ** n > x:
high = mid
else:
low = mid
mid = (high + low) / 2
return mid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment