What better way to check if a number is a perfect power? Need to write an algorithm to check if $ n = a^b $ to $ b > 1 $. There is a mathematical formula or function to calculate this?
I do not know a or b, i know only n.
What better way to check if a number is a perfect power? Need to write an algorithm to check if $ n = a^b $ to $ b > 1 $. There is a mathematical formula or function to calculate this?
I do not know a or b, i know only n.
Copyright © 2021 JogjaFile Inc.
It's easy to see that increasing $b$ decreases $a$ (and vice versa). Since the smallest possible value of $a$ is $a_{\mathrm{min}}=2$, the largest useful value of $b$ to be tested is $b_{\mathrm{max}}=\lfloor\log_2 n\rfloor$. Thus, in order to check if $n$ is a perfect power, you only need to check whether any of its second, third, fourth, ... $b_{\mathrm{max}}$-th roots is an integer. Assuming that your $n$ is (at most) a 64-bit integer, this estimate gives you $b_{\mathrm{max}}<64$, meaning that you wouldn't need to check more than 62 different roots in any case.
There are a few further steps you can take: