C++ Solution using Log


#1

Let given number be N;
So according to question, N=A^§
=> log(N)=P*log(A)
=> P=log(N)/log(A)
and P has to be integer, So log(N)%log(A) ==0
So I will divide both and check if fractional part is very very less i.e 10^-9
If yes then P is an integer.

int Solution::isPower(int A) {
if(A==1)
return 1;

int r=sqrt(A);
for(int i=2;i<=r;i++){
    double p=(double)log((double)A)/log((double)i);
    double intpart, fractpart;
    fractpart = modf(p, &intpart);
    if(fractpart<1e-9){
        return 1;
    }
}
return 0;

}