Find Nth root of a number using Bisection method - GeeksforGeeks

21 Oct, 2021
Given two positive integers N and P. The task is to find the Nth root of P.
Examples: 
Input: P = 1234321, N = 2Output: 1111Explanation: square root of 1234321 is 1111.
Input: P = 123456785, N = 20Output: 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
Java
Python3
C#
Javascript
// C++ program for above approach#include <bits/stdc++.h>using namespace std; // Function that returns the value// of the function at a given value of xdouble f(double x, int p, double num){    return pow(x, p) - num;}// calculating the value// of the differential of the functiondouble f_prime(double x, int p){    return p * pow(x, p - 1);} // The function that returns// the root of given numberdouble root(double num, int p){     // Defining range    // on which answer can be found    double left = -num;    double right = num;     double x;    while (true) {         // finding mid value        x = (left + right) / 2.0;        double value = f(x, p, num);        double prime = f_prime(x, p);        if (value * prime <= 0)            left = x;        else            right = x;        if (value < 0.000001 && value >= 0) {            return x;        }    }} // Driver codeint main(){     double P = 1234321;    int N = 2;     double ans = root(P, N);    cout << ans;}
// Java program for the above approachimport java.util.*; class GFG{     // Function that returns the value  // of the function at a given value of x  static double f(double x, int p, double num)  {      return Math.pow(x, p) - num;  }     // calculating the value  // of the differential of the function  static double f_prime(double x, int p)  {      return p * Math.pow(x, p - 1);  }   // The function that returns  // the root of given number  static double root(double num, int p)  {       // Defining range      // on which answer can be found      double left = -num;      double right = num;       double x;      while (true) {           // finding mid value          x = (left + right) / 2.0;          double value = f(x, p, num);          double prime = f_prime(x, p);          if (value * prime <= 0)              left = x;          else              right = x;          if (value < 0.000001 && value >= 0) {              return x;          }      }  }   // Driver code  public static void main(String args[])  {       double P = 1234321;      int N = 2;       double ans = root(P, N);      System.out.print(ans);  }} // This code is contributed by ihritik
# python program for above approach # Function that returns the value# of the function at a given value of xdef f(x, p, num):     return pow(x, p) - num # calculating the value# of the differential of the functiondef f_prime(x, p):     return p * pow(x, p - 1) # The function that returns# the root of given numberdef root(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.000001 and value >= 0):            return x # Driver codeif __name__ == "__main__":     P = 1234321    N = 2     ans = root(P, N)    print(ans) # This code is contributed by rakeshsahni
// C# program for the above approach using System; public class GFG{     // Function that returns the value  // of the function at a given value of x  static double f(double x, int p, double num)  {      return Math.Pow(x, p) - num;  }     // calculating the value  // of the differential of the function  static double f_prime(double x, int p)  {      return p * Math.Pow(x, p - 1);  }   // The function that returns  // the root of given number  static double root(double num, int p)  {       // Defining range      // on which answer can be found      double left = -num;      double right = num;       double x;      while (true) {           // finding mid value          x = (left + right) / 2.0;          double value = f(x, p, num);          double prime = f_prime(x, p);          if (value * prime <= 0)              left = x;          else              right = x;          if (value < 0.000001 && value >= 0) {              return x;          }      }  }   // Driver code  public static void Main(string []args)  {       double P = 1234321;      int N = 2;       double ans = root(P, N);      Console.Write(ans);  }} // This code is contributed by AnkThon
<script>        // JavaScript Program to implement        // the above approach         // Function that returns the value        // of the function at a given value of x        function f(x, p, num) {            return Math.pow(x, p) - num;        }                 // calculating the value        // of the differential of the function        function f_prime(x, p) {            return p * Math.pow(x, p - 1);        }         // The function that returns        // the root of given number        function root(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) {                    return x;                }            }        }         // 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).Auxilliary Space: O(1).
lokeshpotta20
rakeshsahni
ihritik
ankthon
root
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Segment Tree | Set 1 (Sum of given range)
Modular multiplicative inverse
Count all possible paths from top left to bottom right of a mXn matrix
Fizz Buzz Implementation
Check if a number is Palindrome
Program to multiply two matrices
Merge two sorted arrays with O(1) extra space
Generate all permutation of a set in Python
Count ways to reach the n'th stair