Isaac Emmanuel
15 Jun 2024•EasyC++ / Python / TypeScript

Power of Three

C++, Python and TypeScript solutions for Power of Three.

The Power of Three solutions are below.

Solution files

Python•power-of-three/synced-solution.py
def isPowerOfThree(n: int) -> bool:
    if n <= 0:
        return False
    while n % 3 == 0:
        n //= 3
    return n == 1
C++•power-of-three/synced-solution.cpp
class Solution {
public:
    bool isPowerOfThree(int n) {
        if (n <= 0) return false;
        while (n % 3 == 0) {
            n /= 3;
        }
        return n == 1;
    }
};
TypeScript•power-of-three/synced-solution.ts
function isPowerOfThree(n: number): boolean {
    if (n <= 0) return false;
    while (n % 3 === 0) {
        n /= 3;
    }
    return n === 1;
}