You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classSolution {
boolisPowerOfThree(int n) {
if (n <=1) return n ==1;
return n %3==0&&isPowerOfThree(n ~/3);
}
}
Solution - 5
classSolution {
boolisPowerOfThree(int n) {
if (n <1) returnfalse;
for (; n !=1; n ~/=3) if (n %3!=0) returnfalse;
returntrue;
}
}
Solution - 6
classSolution {
boolisPowerOfThree(int n) {
return n >0&&1162261467% n ==0;
// pow(3, floor(log(INT_MAX)/log(3))) = 1162261467
}
}
Solution - 7
classSolution {
boolisPowerOfThree(int n) {
if (n <1) returnfalse;
String nBase3 ="";
while (n !=0) nBase3 += (n %3).toString();
n ~/=3; // conversion to base 3int i =0;
while (i < nBase3.length -1)
if (nBase3[i++] !='0')
returnfalse; // checking if all digits in base 3 converted number except first one are 0return nBase3[i] =='1'; // check if starting digit is 1
}
}