Question
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?
Code
#include<iostream>
using namespace std;
class Solution {
public:
bool isPowerOfThree(int n) {
double res = log10(n) / log10(3);
return (res - (int)res == 0) ? true:false;
}
};
int main() {
Solution so;
int n = 9;
cout << so.isPowerOfThree(n) << endl;
system("pause");
return 0;
}