Created
August 10, 2020 05:44
-
-
Save kirarpit/c8b5c892801432cd1278d6049a5607ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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