You can try k >= 2 and limit N
with sqrt(w)
, start i
with 2
in the loop. If solution is not found then k = 1, n = w. Also you do not need pow
, you could multiply intermediate product by N
on each iteration.
solved Given an integer, w, find n and k such that n^k = w [closed]