LeetCode:Power of Three
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?
解题思路:
- 用数学公式$\frac{log_{10}n}{log_{10}3}$为整数
- 更多参考:http://blog.csdn.net/ebowtang/article/details/50485622
code
1
2
3
4
5
6
7class Solution {
public:
bool isPowerOfThree(int n) {
double dtmp = log10(n) / log10(3);
return int(dtmp) - dtmp == 0;
}
};