|
@@ -0,0 +1,32 @@
|
|
|
|
|
+#include <iostream>
|
|
|
|
|
+#include <cstdint>
|
|
|
|
|
+#include <cmath>
|
|
|
|
|
+
|
|
|
|
|
+class Solution {
|
|
|
|
|
+public:
|
|
|
|
|
+ bool isPowerOfThree(int n) {
|
|
|
|
|
+ if ( n <= 0 ) { return false; }
|
|
|
|
|
+ if ( n == 1 ) { return true; }
|
|
|
|
|
+ for ( int i = 1; i < n; ++i ) {
|
|
|
|
|
+ long val = pow(3, i);
|
|
|
|
|
+ if ( val == n ) { return true; }
|
|
|
|
|
+ if ( val > n ) { return false; }
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+void r(int n, bool exp) {
|
|
|
|
|
+ std::cout << "Solution().isPowerOfThree(" << n << ") = ";
|
|
|
|
|
+ std::cout << Solution().isPowerOfThree(n);
|
|
|
|
|
+ std::cout << " | exp: " << exp << "\n";
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+int main(void) {
|
|
|
|
|
+ r(27, true);
|
|
|
|
|
+ r(9, false);
|
|
|
|
|
+ r(0, false);
|
|
|
|
|
+ r(-1, false);
|
|
|
|
|
+
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|