# Calculating n-th real root using binary search

Given two number x and n, find n-th root of x.

**Examples:**

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

Input :5 2Output :2.2360679768025875

Input :x = 5, n = 3Output :1.70997594668

In order to calculate n^{th} root of a number, we can use the following procedure.

- If x lies in the range
**[0, 1)**then we set the lower limit**low = x**and upper limit**high = 1**, because for this range of numbers the nth root is always greater than the given number and can never exceed 1.

eg- - Otherwise, we take
**low = 1**and**high = x**. - Declare a variable named
**epsilon**and initialize it for accuracy you need.

Say epsilon=0.01, then we can guarantee that our guess for nth root of the given number will be

correct up to 2 decimal places. - Declare a variable guess and initialize it to
**guess=(low+high)/2.** - Run a loop such that:
- if the
**absolute error**of our**guess**is more than**epsilon**then do:- if
**guess**, then^{n}> x**high=guess** - else
**low=guess** - Making a new better
**guess**i.e.,**guess=(low+high)/2.**

- if
- If the
**absolute error**of our**guess**is less than epsilon then exit the loop.

- if the

** Absolute Error:** Absolute Error can be calculated as

**abs(guess**

^{n}-x)## C++

`// C++ Program to find` `// n-th real root of x` `#include <bits/stdc++.h>` `using` `namespace` `std;` `void` `findNthRoot(` `double` `x, ` `int` `n)` `{` ` ` `// Initialize boundary values` ` ` `double` `low, high;` ` ` `if` `(x >= 0 and x <= 1)` ` ` `{` ` ` `low = x;` ` ` `high = 1;` ` ` `}` ` ` `else` ` ` `{` ` ` `low = 1;` ` ` `high = x;` ` ` `}` ` ` `// Used for taking approximations` ` ` `// of the answer` ` ` `double` `epsilon = 0.00000001;` ` ` `// Do binary search` ` ` `double` `guess = (low + high) / 2;` ` ` `while` `(` `abs` `((` `pow` `(guess, n)) - x) >= epsilon)` ` ` `{` ` ` `if` `(` `pow` `(guess, n) > x)` ` ` `{` ` ` `high = guess;` ` ` `}` ` ` `else` ` ` `{` ` ` `low = guess;` ` ` `}` ` ` `guess = (low + high) / 2;` ` ` `}` ` ` `cout << fixed << setprecision(16) << guess;` `}` `// Driver code` `int` `main()` `{` ` ` `double` `x = 5;` ` ` `int` `n = 2;` ` ` `findNthRoot(x, n);` `}` `// This code is contributed` `// by Subhadeep` |

## Java

`// Java Program to find n-th real root of x` `class` `GFG` `{` ` ` `static` `void` `findNthRoot(` `double` `x, ` `int` `n)` ` ` `{` ` ` `// Initialize boundary values` ` ` `double` `low, high;` ` ` `if` `(x >= ` `0` `&& x <= ` `1` `)` ` ` `{` ` ` `low = x;` ` ` `high = ` `1` `;` ` ` `}` ` ` `else` ` ` `{` ` ` `low = ` `1` `;` ` ` `high = x;` ` ` `}` ` ` `// used for taking approximations` ` ` `// of the answer` ` ` `double` `epsilon = ` `0.00000001` `;` ` ` `// Do binary search` ` ` `double` `guess = (low + high) / ` `2` `;` ` ` `while` `(Math.abs((Math.pow(guess, n)) - x)` ` ` `>= epsilon)` ` ` `{` ` ` `if` `(Math.pow(guess, n) > x)` ` ` `{` ` ` `high = guess;` ` ` `}` ` ` `else` ` ` `{` ` ` `low = guess;` ` ` `}` ` ` `guess = (low + high) / ` `2` `;` ` ` `}` ` ` `System.out.println(guess);` ` ` `}` ` ` `// Driver code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `double` `x = ` `5` `;` ` ` `int` `n = ` `2` `;` ` ` `findNthRoot(x, n);` ` ` `}` `}` `// This code is contributed` `// by mits` |

## Python3

`# Python Program to find n-th real root` `# of x` `def` `findNthRoot(x, n):` ` ` `# Initialize boundary values` ` ` `x ` `=` `float` `(x)` ` ` `n ` `=` `int` `(n)` ` ` `if` `(x >` `=` `0` `and` `x <` `=` `1` `):` ` ` `low ` `=` `x` ` ` `high ` `=` `1` ` ` `else` `:` ` ` `low ` `=` `1` ` ` `high ` `=` `x` ` ` `# used for taking approximations` ` ` `# of the answer` ` ` `epsilon ` `=` `0.00000001` ` ` `# Do binary search` ` ` `guess ` `=` `(low ` `+` `high) ` `/` `2` ` ` `while` `abs` `(guess ` `*` `*` `n ` `-` `x) >` `=` `epsilon:` ` ` `if` `guess ` `*` `*` `n > x:` ` ` `high ` `=` `guess` ` ` `else` `:` ` ` `low ` `=` `guess` ` ` `guess ` `=` `(low ` `+` `high) ` `/` `2` ` ` `print` `(guess)` `# Driver code` `x ` `=` `5` `n ` `=` `2` `findNthRoot(x, n)` |

## C#

`// C# Program to find n-th real root of x` `using` `System;` `public` `class` `GFG {` ` ` `static` `void` `findNthRoot(` `double` `x, ` `int` `n)` ` ` `{` ` ` `// Initialize boundary values` ` ` `double` `low, high;` ` ` `if` `(x >= 0 && x <= 1)` ` ` `{` ` ` `low = x;` ` ` `high = 1;` ` ` `}` ` ` `else` ` ` `{` ` ` `low = 1;` ` ` `high = x;` ` ` `}` ` ` `// used for taking approximations` ` ` `// of the answer` ` ` `double` `epsilon = 0.00000001;` ` ` `// Do binary search` ` ` `double` `guess = (low + high) / 2;` ` ` `while` `(Math.Abs((Math.Pow(guess, n)) - x)` ` ` `>= epsilon)` ` ` `{` ` ` `if` `(Math.Pow(guess, n) > x)` ` ` `{` ` ` `high = guess;` ` ` `}` ` ` `else` ` ` `{` ` ` `low = guess;` ` ` `}` ` ` `guess = (low + high) / 2;` ` ` `}` ` ` `Console.WriteLine(guess);` ` ` `}` ` ` `// Driver code` ` ` `static` `public` `void` `Main()` ` ` `{` ` ` `double` `x = 5;` ` ` `int` `n = 2;` ` ` `findNthRoot(x, n);` ` ` `}` `}` `// This code is contributed by akt_mit` |

## Javascript

`<script>` ` ` `// Javascript Program to find n-th` ` ` `// real root of x` ` ` ` ` `function` `findNthRoot(x, n)` ` ` `{` ` ` ` ` `// Initialize boundary values` ` ` `let low, high;` ` ` `if` `(x >= 0 && x <= 1)` ` ` `{` ` ` `low = x;` ` ` `high = 1;` ` ` `}` ` ` `else` ` ` `{` ` ` `low = 1;` ` ` `high = x;` ` ` `}` ` ` ` ` `// used for taking approximations` ` ` `// of the answer` ` ` `let epsilon = 0.00000001;` ` ` ` ` `// Do binary search` ` ` `let guess = parseInt((low + high) / 2, 10);` ` ` `while` `(Math.abs((Math.pow(guess, n)) - x)` ` ` `>= epsilon)` ` ` `{` ` ` `if` `(Math.pow(guess, n) > x)` ` ` `{` ` ` `high = guess;` ` ` `}` ` ` `else` ` ` `{` ` ` `low = guess;` ` ` `}` ` ` `guess = (low + high) / 2;` ` ` `}` ` ` ` ` `document.write(guess);` ` ` `}` ` ` ` ` `let x = 5;` ` ` `let n = 2;` ` ` `findNthRoot(x, n);` ` ` `</script>` |

**Output**

2.2360679768025875

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 = 3 Absolute error = |3^{2}- 5| = 4 > epsilon guess^{2}= 9 > 5(x) then high = guess --> high = 3Second Guess:guess = (1 + 3) / 2 = 2 Absolute error = |2^{2}- 5| = 1 > epsilon guess^{2}= 4 > 5(x) then low = guess --> low = 2Third Guess:guess = (2 + 3) / 2 = 2.5 Absolute error = |2.5^{2}- 5| = 1.25 > epsilon guess^{2}= 6.25 > 5(x) then high = guess --> high = 2.5 and proceeding so on we will get the correct up to 2 decimal places i.e., = 2.23600456 We will ignore the digits after 2 decimal places since they may or may not be correct.