Skip to content

Latest commit

 

History

History
36 lines (26 loc) · 652 Bytes

326.md

File metadata and controls

36 lines (26 loc) · 652 Bytes

326 Power of Three

Description

link


Solution

See Code


Code

class Solution:
    def isPowerOfThree(self, n: 'int') -> 'bool':
        if not n:
            return False
        if n == 1:
            return True
        if n % 3 == 0:
            return self.isPowerOfThree(n//3)
        else:
            return False

# Follow up:
# Could you do it without using any loop / recursion?

class Solution:
    def isPowerOfThree(self, n: 'int') -> 'bool':
        # 1162261467 is 3^19,  3^20 is bigger than int  
        return n > 0 and 1162261467 % n == 0