Given a positive integer N, write a function to find if it is a power of three or not.
 
Example 1:
Input:
N = 3
Output:
Yes
Explanation:
3^{1} is a power of 3.
 
Example 2:
Input:
N = 5
Output:
No
Explanation:
5 is not a power of 3.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function isPowerof3() which takes an integer N and returns "Yes" if N is a power of 3, else "No" if N is not a power of 3.
 
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
 
Constraints:
1<=N<=(32-bit largest integer)

Source: geeksforgeeks | Difficulty: EASY
Tags: Mathematics

Starter code: #User function Template for python3

class Solution:

    def isPowerof3 (ob,N):

        # code here 

Solutions:
import math

class Solution:

	def isPowerof3(ob, N):
		if N > 0 and 3 ** round(math.log(N, 3)) == N:
			return 'Yes'
		return 'No'


class Solution:

	def isPowerof3(ob, N):
		if N == 1:
			return 'Yes'
		if N == 0:
			return 'Yes'
		if N % 3 != 0:
			return 'No'
		return Solution.isPowerof3(ob, N / 3)


import math

class Solution:

	def isPowerof3(ob, n):
		if n <= 0:
			return 'No'
		if 3 ** round(math.log(n, 3)) == n:
			return 'Yes'
		return 'No'


from math import log10

class Solution:

	def isPowerof3(ob, N):
		if N <= 0:
			return 'No'
		return 'Yes' if (log10(N) / log10(3)).is_integer() else 'No'


class Solution:

	def isPowerof3(ob, N):
		l = []
		for i in range(32):
			l.append(pow(3, i))
		if N in l:
			return 'Yes'
		else:
			return 'No'


import math

class Solution:

	def isPowerof3(ob, N):
		a = math.log10(N)
		b = math.log10(3)
		c = a / b
		if c == int(c):
			return 'Yes'
		return 'No'


class Solution:

	def isPowerof3(ob, N):
		if N == 1:
			return 'Yes'
		elif N > 1:
			r = N
			while r > 1:
				r = r / 3
			if r == 1:
				return 'Yes'
			elif r < 1:
				return 'No'


class Solution:

	def isPowerof3(ob, N):
		import math
		c = round(math.log(N) / math.log(3))
		if N == pow(3, c):
			return 'Yes'
		else:
			return 'No'


class Solution:

	def isPowerof3(ob, N):
		i = 1
		while i < N:
			i = 3 * i
		if i == N:
			return 'Yes'
		else:
			return 'No'


class Solution:

	def isPowerof3(ob, N):
		if N == 1:
			return 'Yes'
		lastDigit = N % 10
		map = [0] * 1000
		map[3] = 1
		map[9] = 2
		map[7] = 3
		map[1] = 4
		if map[lastDigit] == None:
			return 'No'
		power = map[lastDigit]
		powerOfThree = pow(3, power)
		while powerOfThree <= N:
			if powerOfThree == N:
				return 'Yes'
			power = power + 4
			powerOfThree = pow(3, power)
		return 'No'


import math

class Solution:

	def isPowerof3(ob, N):
		y = math.log10(N) / math.log10(3)
		if math.floor(y) == math.ceil(y):
			return 'Yes'
		else:
			return 'No'


class Solution:

	def isPowerof3(ob, n):
		while n > 1:
			n = n / 3
		if n == 1:
			return 'Yes'
		return 'No'


class Solution:

	def isPowerof3(ob, N):
		for x in range(2000000):
			if 3 ** x == N:
				return 'Yes'
			elif 3 ** x > N:
				return 'No'


class Solution:

	def isPowerof3(ob, n):
		if n > 0 and 3486784401 % n == 0:
			return 'Yes'
		else:
			return 'No'


class Solution:

	def isPowerof3(ob, n):
		while n % 3 == 0:
			n /= 3
		if n == 1:
			return 'Yes'
		else:
			return 'No'
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isPowerof3(N))


from math import ceil

class Solution:

	def isPowerof3(ob, N):
		if N == 1:
			return 'Yes'
		elif N < 1:
			return 'No'
		else:
			return ob.isPowerof3(N / 3)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isPowerof3(N))


import math

class Solution:

	def isPowerof3(ob, N):
		if N > 0 and 1162261467 % N == 0:
			return 'Yes'
		else:
			return 'No'
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isPowerof3(N))


import math

class Solution:

	def isPowerof3(ob, N):
		MAX_INT = 2 ** 32 // 2 - 1
		x = 3 ** int(math.log(MAX_INT, 3))
		if N > 0 and x % N == 0:
			return 'Yes'
		else:
			return 'No'
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isPowerof3(N))


import math

class Solution:

	def isPowerof3(ob, N):
		log = round(math.log(N, 3), 10)
		if log.is_integer():
			return 'Yes'
		return 'No'
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isPowerof3(N))


import math

class Solution:

	def isPowerof3(ob, N):
		while N > 1:
			if N % 3 != 0:
				return 'No'
			N = N // 3
		return 'Yes'


import math

class Solution:

	def isPowerof3(ob, N):
		if N == 0:
			return False
		log = math.log10(N) / math.log10(3)
		return 'Yes' if math.ceil(log) == math.floor(log) else 'No'


class Solution:

	def isPowerof3(ob, n):
		if n == 1:
			return 'Yes'
		if n <= 0 or n % 3 != 0:
			return 'No'
		return ob.isPowerof3(n // 3)


class Solution:

	def po3(self, n):
		if n <= 0:
			return False
		if n % 3 == 0:
			return ob.po3(n // 3)
		if n == 1:
			return True
		return False

	def isPowerof3(ob, N):
		if ob.po3(N):
			return 'Yes'
		return 'No'


class Solution:

	def isPowerof3(ob, x):
		n = 2147483647
		a = []
		i = 1
		while i < n:
			a.append(i)
			i = i * 3
		if x in a:
			return 'Yes'
		else:
			return 'No'


class Solution:

	def isPowerof3(ob, n):
		from math import floor, ceil, log10
		if n <= 0:
			return 'No'
		x = log10(n) / log10(3)
		if floor(x) == ceil(x):
			return 'Yes'
		else:
			return 'No'


class Solution:

	def isPowerof3(self, n):
		if n == 1:
			return 'Yes'
		if n == 0:
			return 'No'
		if n % 3:
			return 'No'
		return self.isPowerof3(n // 3)


from math import log, ceil, floor

class Solution:

	def isPowerof3(ob, N):
		if N < 1:
			return 'No'
		c = ceil(round(log(N, 3), 3))
		f = floor(round(log(N, 3), 3))
		if c == f:
			return 'Yes'
		return 'No'


class Solution:

	def isPowerof3(ob, n):
		if n <= 0:
			return 'No'
		elif n == 1:
			return 'Yes'
		else:
			while n > 1:
				if n % 3 == 0:
					n = n // 3
				else:
					return 'No'
		return 'Yes'


class Solution:

	def isPowerof3(ob, N):
		return 'Yes' if 1162261467 % N == 0 else 'No'


import math

class Solution:

	def isPowerof3(ob, N):
		a = math.ceil(math.log(N, 3))
		if pow(3, a) == N:
			return 'Yes'
		else:
			return 'No'