Power of Three

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?

Tags

  • Mathematics

Thought

Reference: http://bookshadow.com/weblog/2016/01/08/leetcode-power-three/

Code

class Solution(object):
    def isPowerOfThree(self, n):
        """
        :type n: int
        :rtype: bool
        """
        return n > 0 and 3 ** round(math.log(n,3)) == n

results matching ""

    No results matching ""