Given an integer, write a function to determine if it is a power of three.
Follow up:
Could you do it without using any loop / recursion?
=========================================常规方法==================================
public boolean isPowerOfThree(int n) {
if(n<=0){
return false;
}
// double num=Math.log(n)/Math.log(3); log()与log10()与log1p()的区别
double num=Math.log10(n)/Math.log10(3);
if(num%1==0){
return true;
}else{
return false;
}
}
=============================流氓方法=================================
public class Solution {
public boolean isPowerOfThree(int n) {
// 1162261467 is 3^19, 3^20 is bigger than int
return ( n>0 && 1162261467%n==0);
}
}
//1162261467是32位系统中,3的最高次幂19