Open In App

N-th root of a number

Last Updated : 12 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given two numbers n and m, find the n-th root of m. In mathematics, the n-th root of a number m is a real number that, when raised to the power of n, gives m. If no such real number exists, return -1.

Examples: 

Input: n = 2, m = 9
Output: 3
Explanation: 32 = 9

Input: n = 3, m = 9
Output: -1
Explanation: 3rd root of 9 is not integer.

Approach - Using Binary Search

The approach uses binary search to find the n-th root of a number m. The search range is between 1 and m, and in each iteration, the middle value mid is raised to the power of n.

  • If mid^n equals m, mid is the root.
  • If mid^n is greater than m, the search range is narrowed to smaller values, and
  • if mid^n is smaller than m, the search range is adjusted to larger values.
  • The process continues until the root is found or the range is exhausted. If no exact root is found, the result is -1
C++
#include <iostream>
using namespace std;

int nthRoot(int n, int m) {
    if (n == 1) {
        return m;
    }

    long long int low = 1LL, high = m, mid;
    long long int ans = -1;
    while (low <= high) {
        mid = (low + high) / 2;
        long long int x = mid;
        for (int i = 1; i < n; i++) {
            x *= mid;
            if (x > m * 1LL)
                break;
        }
        if (x == m * 1LL) {
            ans = mid;
            break;
        } else if (x > m)
            high = mid - 1;
        else
            low = mid + 1;
    }
    return int(ans);
}

int main() {
    int n = 1, m = 14; 
    
    int result = nthRoot(n, m);
    
    cout << result << endl;
    
    return 0;
}
C
#include <stdio.h>

int nthRoot(int n, int m) {
    if (n == 1) {
        return m;
    }

    long long int low = 1LL, high = m, mid;
    long long int ans = -1;
    while (low <= high) {
        mid = (low + high) / 2;
        long long int x = mid;
        for (int i = 1; i < n; i++) {
            x *= mid;
            if (x > m * 1LL)
                break;
        }
        if (x == m * 1LL) {
            ans = mid;
            break;
        } else if (x > m)
            high = mid - 1;
        else
            low = mid + 1;
    }
    return (int)ans;
}

int main() {
    int n = 1, m = 14;
    int result = nthRoot(n, m);
    printf("%d\n", result);
    return 0;
}
Java
public class GfG {
    public static int nthRoot(int n, int m) {
        if (n == 1) {
            return m;
        }

        long low = 1, high = m, mid;
        long ans = -1;
        while (low <= high) {
            mid = (low + high) / 2;
            long x = mid;
            for (int i = 1; i < n; i++) {
                x *= mid;
                if (x > m)
                    break;
            }
            if (x == m) {
                ans = mid;
                break;
            } else if (x > m)
                high = mid - 1;
            else
                low = mid + 1;
        }
        return (int)ans;
    }

    public static void main(String[] args) {
        int n = 1, m = 14;
        int result = nthRoot(n, m);
        System.out.println(result);
    }
}
Python
def nthRoot(n, m):
    if n == 1:
        return m

    low, high = 1, m
    ans = -1
    while low <= high:
        mid = (low + high) // 2
        x = mid
        for i in range(1, n):
            x *= mid
            if x > m:
                break
        if x == m:
            ans = mid
            break
        elif x > m:
            high = mid - 1
        else:
            low = mid + 1
    return int(ans)

n = 1
m = 14
result = nthRoot(n, m)
print(result)
C#
using System;

class GfG{
    static int NthRoot(int n, int m) {
        if (n == 1) {
            return m;
        }

        long low = 1, high = m, mid;
        long ans = -1;
        while (low <= high) {
            mid = (low + high) / 2;
            long x = mid;
            for (int i = 1; i < n; i++) {
                x *= mid;
                if (x > m)
                    break;
            }
            if (x == m) {
                ans = mid;
                break;
            } else if (x > m)
                high = mid - 1;
            else
                low = mid + 1;
        }
        return (int)ans;
    }

    static void Main() {
        int n = 1, m = 14;
        int result = NthRoot(n, m);
        Console.WriteLine(result);
    }
}
JavaScript
function nthRoot(n, m) {
    if (n === 1) {
        return m;
    }

    let low = 1, high = m, mid;
    let ans = -1;
    while (low <= high) {
        mid = Math.floor((low + high) / 2);
        let x = mid;
        for (let i = 1; i < n; i++) {
            x *= mid;
            if (x > m) break;
        }
        if (x === m) {
            ans = mid;
            break;
        } else if (x > m)
            high = mid - 1;
        else
            low = mid + 1;
    }
    return ans;
}

const n = 1, m = 14;
const result = nthRoot(n, m);
console.log(result);

Output
14

Time Complexity: O(n*log m)
Auxiliary Space: O(1)

Approach - Using Newton's Method

This problem involves finding the real-valued function A^{1/N}, which can be solved using Newton's method. The method begins with an initial guess and iteratively refines the result.

By applying Newton's method, we derive a relation between consecutive iterations:

According to Newton's method:

\frac{f(x_k)}{f'(x_k)}

Here, we define the function f(x) as:

f(x) = xN- A

The derivative of f(x), denoted f'(x), is:

N\cdot x^{N - 1}

The value xk represents the approximation at the k-th iteration. By substituting f(x) and f'(x) into the Newton's method formula, we obtain the following update relation:

\frac{1}{N} \left( (N - 1) \cdot x_k + \frac{A}{x_k^{N - 1}} \right)

Using this relation, the problem can be solved by iterating over successive values of x until the difference between consecutive iterations is smaller than the desired accuracy.

C++
#include <bits/stdc++.h>
using namespace std;

int nthRoot(int m, int n)
{
    if (n == 1) {
        return m;  
    }
    
    double xPre = rand() % 10 + 1;  

    // Smaller eps denotes more accuracy
    double eps = 1e-3;

    // Initializing difference between two roots
    double delX = INT_MAX;

    // xK denotes current value of x
    double xK;

    // Loop until we reach the desired accuracy
    while (delX > eps)
    {
        // Calculating the current value from the previous value by Newton's method
        xK = ((n - 1.0) * xPre + (double)m / pow(xPre, n - 1)) / (double)n;
        delX = abs(xK - xPre);
        xPre = xK;
    }

    return (int)round(xK); 
}

int main() {
    int n = 1, m = 14;  
    int nthRootValue = nthRoot(m, n);

    cout << nthRootValue << endl;

    return 0;
}
C
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h> 

int nthRoot(int m, int n) {
    if (n == 1) {
        return m;
    }

    double xPre = rand() % 10 + 1;  

    // Smaller eps denotes more accuracy
    double eps = 1e-3;

    // Initializing difference between two roots
    double delX = 1e6;  

    // xK denotes current value of x
    double xK;

    // Loop until we reach the desired accuracy
    while (delX > eps) {
        
        // Calculating the current value from the previous value by Newton's method
        xK = ((n - 1.0) * xPre + (double)m / pow(xPre, n - 1)) / (double)n;
        delX = fabs(xK - xPre);
        xPre = xK;
    }

    return (int)round(xK); 
}

int main() {
    int n = 1, m = 14;  

    int nthRootValue = nthRoot(m, n);

    printf("%d\n", nthRootValue);

    return 0;
}
Java
import java.lang.Math;
import java.util.Random;

public class GfG {

    public static int nthRoot(int m, int n) {

        if (n == 1) {
            return m; 
        }

        Random rand = new Random();

        // Initial guess (use m / 2 or another logic to get a reasonable starting point)
        double xPre = rand.nextInt(10) + 1;  

        // Smaller eps denotes more accuracy
        double eps = 1e-3;

        // Initializing difference between two roots
        double delX = Double.MAX_VALUE;

        // xK denotes current value of x
        double xK = xPre;  

        // Loop until we reach the desired accuracy
        while (delX > eps) {
            
            // Calculating the current value from the previous value by Newton's method
            xK = ((n - 1.0) * xPre + (double)m / Math.pow(xPre, n - 1)) / (double)n;
            delX = Math.abs(xK - xPre);
            xPre = xK;
        }

        return (int)Math.round(xK);
    }

    public static void main(String[] args) {
        int n = 1, m = 14; 

        int nthRootValue = nthRoot(m, n);

        System.out.println(nthRootValue);
    }
}
Python
import random
import math

def nthRoot(m, n):
    if n == 1:
        return m  
        
    xPre = random.randint(1, 10)

    # Smaller eps denotes more accuracy
    eps = 1e-3

    # Initializing difference between two roots
    delX = float('inf')

    # xK denotes current value of x
    xK = 0.0

    # Loop until we reach the desired accuracy
    while delX > eps:
        
        # Calculating the current value from the previous value by Newton's method
        xK = ((n - 1.0) * xPre + m / (xPre ** (n - 1))) / n
        delX = abs(xK - xPre)
        xPre = xK

    return round(xK)

if __name__ == '__main__':
    n = 1
    m = 14  

    nthRootValue = nthRoot(m, n)

    print(nthRootValue)
C#
using System;

class GfG {
    public static int nthRoot(int m, int n) {
        
        if (n == 1) {
            return m;  
        }

        Random rand = new Random();
        double xPre = rand.Next(1, 11); 

        // Smaller eps denotes more accuracy
        double eps = 1e-3;

        // Initializing difference between two roots
        double delX = double.MaxValue;

        // xK denotes current value of x, initialize with xPre
        double xK = xPre;  

        // Loop until we reach the desired accuracy
        while (delX > eps) {
            
            // Calculating the current value from the previous value by Newton's method
            xK = ((n - 1.0) * xPre + (double)m / Math.Pow(xPre, n - 1)) / (double)n;
            delX = Math.Abs(xK - xPre);
            xPre = xK;
        }

        // Return the integer part of the result (rounded to nearest integer)
        return (int)Math.Round(xK);
    }

    // Driver code to test the nthRoot method
    public static void Main() {
        int n = 1, m = 14;  
        int nthRootValue = nthRoot(m, n);

        Console.WriteLine(nthRootValue);
    }
}
JavaScript
function nthRoot(m, n) {
    if (n === 1) {
        return m;  
    }

    let xPre = Math.floor(Math.random() * 10) + 1;  

    // Smaller eps denotes more accuracy
    const eps = 1e-3;

    // Initializing difference between two roots
    let delX = Infinity;

    // xK denotes current value of x
    let xK;

    // Loop until we reach the desired accuracy
    while (delX > eps) {
        // Calculating the current value from the previous value by Newton's method
        xK = ((n - 1) * xPre + m / Math.pow(xPre, n - 1)) / n;
        delX = Math.abs(xK - xPre);
        xPre = xK;
    }

    // Return the integer part of the result
    return Math.round(xK);
}

const n = 1, m = 14;  

const nthRootValue = nthRoot(m, n);

console.log(nthRootValue);

Output
14

Time Complexity: O(log(eps)), where eps is the desired accuracy.
Space Complexity: O(1)


 


Next Article

Similar Reads