Given two positive integers N and P. The task is to find the Nth root of P.
Examples:
Input: P = 1234321, N = 2
Output: 1111
Explanation: square root of 1234321 is 1111.Input: P = 123456785, N = 20
Output: 2.53849
Approach: There are various ways to solve the given problem. Here the below algorithm is based on Mathematical Concept called Bisection Method for finding roots. To find the N-th power root of a given number P we will form an equation is formed in x as ( xp – P = 0 ) and the target is to find the positive root of this equation using the Bisection Method.
How Bisection Method works? 
Take an interval (a, b) such that its already known that the root is existing in that interval. After this find the mid of the interval and examine the value of function and it’s derivative at x = mid.
- If the value of function is 0 that means root is found
- If the value of the function is positive and its derivative is negative that means the root is lying in the right half.
- If the value of the function is positive and its derivative is positive that means the root is lying in the left half.
Below is the implementation of the above approach:
C++14
| // C++ program for above approach#include <bits/stdc++.h>usingnamespacestd;// Function that returns the value// of the function at a given value of xdoublef(doublex, intp, doublenum){    returnpow(x, p) - num;}// calculating the value// of the differential of the functiondoublef_prime(doublex, intp){    returnp * pow(x, p - 1);}// The function that returns// the root of given numberdoubleroot(doublenum, intp){    // Defining range    // on which answer can be found    doubleleft = -num;    doubleright = num;    doublex;    while(true) {        // finding mid value        x = (left + right) / 2.0;        doublevalue = f(x, p, num);        doubleprime = f_prime(x, p);        if(value * prime <= 0)            left = x;        else            right = x;        if(value < 0.000001 && value >= 0) {            returnx;        }    }}// Driver codeintmain(){    doubleP = 1234321;    intN = 2;    doubleans = root(P, N);    cout << ans;} | 
Java
| // Java program for the above approachimportjava.util.*;classGFG {    // Function that returns the value  // of the function at a given value of x  staticdoublef(doublex, intp, doublenum)  {      returnMath.pow(x, p) - num;  }    // calculating the value  // of the differential of the function  staticdoublef_prime(doublex, intp)  {      returnp * Math.pow(x, p - 1);  }  // The function that returns  // the root of given number  staticdoubleroot(doublenum, intp)  {      // Defining range      // on which answer can be found      doubleleft = -num;      doubleright = num;      doublex;      while(true) {          // finding mid value          x = (left + right) / 2.0;          doublevalue = f(x, p, num);          doubleprime = f_prime(x, p);          if(value * prime <= 0)              left = x;          else              right = x;          if(value < 0.000001&& value >= 0) {              returnx;          }      }  }  // Driver code  publicstaticvoidmain(String args[])  {      doubleP = 1234321;      intN = 2;      doubleans = root(P, N);      System.out.print(ans);  }}// This code is contributed by ihritik | 
Python3
| # python program for above approach# Function that returns the value# of the function at a given value of xdeff(x, p, num):    returnpow(x, p) -num# calculating the value# of the differential of the functiondeff_prime(x, p):    returnp *pow(x, p -1)# The function that returns# the root of given numberdefroot(num, p):    # Defining range    # on which answer can be found    left =-num    right =num    x =0    while(True):        # finding mid value        x =(left +right) /2.0        value =f(x, p, num)        prime =f_prime(x, p)        if(value *prime <=0):            left =x        else:            right =x        if(value < 0.000001andvalue >=0):            returnx# Driver codeif__name__ =="__main__":    P =1234321    N =2    ans =root(P, N)    print(ans)# This code is contributed by rakeshsahni | 
C#
| // C# program for the above approachusingSystem;publicclassGFG {    // Function that returns the value  // of the function at a given value of x  staticdoublef(doublex, intp, doublenum)  {      returnMath.Pow(x, p) - num;  }    // calculating the value  // of the differential of the function  staticdoublef_prime(doublex, intp)  {      returnp * Math.Pow(x, p - 1);  }  // The function that returns  // the root of given number  staticdoubleroot(doublenum, intp)  {      // Defining range      // on which answer can be found      doubleleft = -num;      doubleright = num;      doublex;      while(true) {          // finding mid value          x = (left + right) / 2.0;          doublevalue = f(x, p, num);          doubleprime = f_prime(x, p);          if(value * prime <= 0)              left = x;          else              right = x;          if(value < 0.000001 && value >= 0) {              returnx;          }      }  }  // Driver code  publicstaticvoidMain(string[]args)  {      doubleP = 1234321;      intN = 2;      doubleans = root(P, N);      Console.Write(ans);  }}// This code is contributed by AnkThon | 
Javascript
| <script>        // JavaScript Program to implement        // the above approach        // Function that returns the value        // of the function at a given value of x        functionf(x, p, num) {            returnMath.pow(x, p) - num;        }                // calculating the value        // of the differential of the function        functionf_prime(x, p) {            returnp * Math.pow(x, p - 1);        }        // The function that returns        // the root of given number        functionroot(num, p) {            // Defining range            // on which answer can be found            let left = -num;            let right = num;            let x;            while(true) {                // finding mid value                x = (left + right) / 2.0;                let value = f(x, p, num);                let prime = f_prime(x, p);                if(value * prime <= 0)                    left = x;                else                    right = x;                if(value < 0.000001 && value >= 0) {                    returnx;                }            }        }        // Driver code        let P = 1234321;        let N = 2;        let ans = Math.floor(root(P, N));        document.write(ans);// This code is contributed by Potta Lokesh    </script> | 
1111
Time Complexity: O(log P).
Auxiliary Space: O(1).
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!


 
                                    







