Elegant C++ Solution


#1

#include<bits/stdc++.h>
int Solution::isPower(int A) {
if(A==1)
return 1;
for(int i=2;i<=sqrt(A);i++)
{
float a=log(A)/log(i);
if(floor(a)==a)
return 1;
}
return 0;


#2

Why doesn’t it work with double instead of float?


#3

@ devanshanandtiwari_5 Did you figure it out?


#4

It might be because if you use (double==int) in IF condition it will always return false but thats not the case if you use (float==int) =>(a==floor(a)) but if you write like this
double a=log(A)/logi;
int b=(int) a;
if(floor(a)==b)
return 1;
This will work.Again if you use if(a==b) this wont work becuase its (double==int).
Hope this helps!!!