![]() |
VOOZH | about |
Given two number x and n, find n-th root of x.
Examples:
Input : 5 2
Output : 2.2360679768025875Input : x = 5, n = 3
Output : 1.70997594668
In order to calculate nth root of a number, we can use the following procedure.
Absolute Error can be calculated as abs(guessn -x)
2.2360679768025875
Time Complexity: O( log( x * 10d)*logguess(n) )
Auxiliary Space: O(1)
Here d is the number of decimal places upto which we want the result accurately.
Explanation of first example with epsilon = 0.01
Since taking too small value of epsilon as taken in our program might not be feasible for explanation because it will increase the number of steps drastically so for the sake of simplicity we are taking epsilon = 0.01 The above procedure will work as follows: Say we have to calculate the then x = 5, low = 1, high = 5.Taking epsilon = 0.01First Guess:guess = (1 + 5) / 2 = 3Absolute error = |32 - 5| = 4 > epsilonguess2 = 9 > 5(x) then high = guess --> high = 3Second Guess:guess = (1 + 3) / 2 = 2Absolute error = |22 - 5| = 1 > epsilonguess2 = 4 > 5(x) then low = guess --> low = 2Third Guess:guess = (2 + 3) / 2 = 2.5Absolute error = |2.52 - 5| = 1.25 > epsilonguess2 = 6.25 > 5(x) then high = guess --> high = 2.5and proceeding so on we will get the correct up to 2 decimal places i.e., = 2.23600456We will ignore the digits after 2 decimal places since they may or may not be correct.