/**
* @param {number} n
* @return {boolean}
*/
var isPowerOfThree = function(n) {
if (n === 0) return false;
if (n === 1) return true;
return n % 3 === 0 && isPowerOfThree(Math.floor(n / 3));
};
// iterative Ways
var isPowerOfThree = function(n) {
if (n > 1) {
while (n % 3 === 0) {
n = Math.floor(n / 3);
}
}
return n === 1;
};
leetcode
challenge
Here is the link for the problem:
https://leetcode.com/problems/power-of-three/
Top comments (0)