Nth Root of M
Nth Root of M is a GFG problem (Medium). Given two integers n and m, return the whole number x with xⁿ = m. If no whole number works, return −1.
- The limits here are tiny (
nup to 9,mup to 20), so even trying everyxpasses there. The common interview version allowsmup to 10⁹ andnup to 30, and the method on this page is built for that. m = 0is allowed, and its root is 0.
- 1 <= n <= 9
- 0 <= m <= 20
Intuition
As x grows, xⁿ grows too, so the candidates 0, 1, …, m are already sorted by their powers. That turns the question into a binary search over x: work out midⁿ, and if it is too small the root must be larger, if it is too big the root must be smaller. Each guess halves the range, so about log₂ m guesses are enough.
The one trap is the power itself. A value like (5 × 10⁸)³⁰ does not fit in any integer type. So stop multiplying as soon as the product passes m. Past that point the exact value does not matter, only that it is too big.
"Find the whole number x with f(x) = target" where f only grows as x grows is binary search on the answer. Square root and cube root are the same problem with n fixed.
Approach
Before reading on, check that xⁿ only grows as x grows, and decide the smallest and largest value the root could have. Then work out how to tell "midⁿ is bigger than m" without computing a number that overflows. Aim for O(n log m).
Search x between 0 and m
Set lo = 0 and hi = m. For x ≥ 1, xⁿ is at least x, so the root is never larger than m. Starting at 0 covers m = 0.
Raise mid to the nth power with a cap
Multiply p by mid up to n times, and break out as soon as p > m. Before each multiply p is at most m, so even with m = 10⁹ the product stays under 10¹⁸ and fits in a 64-bit integer.
Compare the power with m
If p == m, mid is the root, so return it. If p < m, every x up to mid is too small, so set lo = mid + 1. If p > m, every x from mid up is too big, so set hi = mid - 1.
An empty range means −1
When lo passes hi, m lies strictly between two neighbouring powers kⁿ and (k + 1)ⁿ, so no whole number has m as its nth power.
Code in Python, C++ and Java
Common pitfalls
Trusting a floating-point root
x = int(m ** (1 / n))
binary search with exact integer powers
For n = 3, m = 1000, 1000 ** (1 / 3) is 9.999999999999998, which int turns into 9. Since 9³ is not 1000, the code returns −1 for a number whose cube root is exactly 10.
Computing the whole power before comparing
for k in range(n):
p *= midfor k in range(n):
p *= mid
if p > m:
breakIn the interview version with n = 30, m = 10⁹, the first guess is 5 × 10⁸, and its 30th power wraps around in a 64-bit integer. The wrapped value can look small, sending the search the wrong way. (Python's big integers survive this but get slow.)
Edge cases
The root is m itself for every n. lo starts at 0, so 0 is a candidate, and 1 is found on the second guess at most.
Every m is its own first root. The search narrows until mid = m, where the power equals m.
Complexity
Nth Root of M FAQ
How do I get the floor of the root when it is not exact?
Return hi instead of −1 after the loop. The search ends with hi on the largest x whose power is below m. With n = 2 this is Sqrt(x).
Is Newton's method faster?
It needs fewer steps, but it works with real numbers. You still have to round its result and check xⁿ = m with integers, so for a yes-or-no answer binary search is simpler to get right.