Given a positive integer N, The task is to find if it is a power of eight or not.
Example 1: 
Input: 64
Output: Yes
Explanation: 64 is power of 8.
Example 2:
Input: 75
Output: No
Explanation: 75 is not a power of 8.
Your Task:
You don't need to read or print anything. your task is to complete the function is_power_of_eight() which takes N as the input parameter and returns "Yes" if N is power of eight otherwise returns "No"(Wihtout quotes).
Expected Time Complexity: O(log(N))
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{18}

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Starter code: #User function Template for python3

class Solution:
	def is_power_of_eight(self, n):
		# Code here

Solutions:
class Solution:

	def is_power_of_eight(self, n):
		(oc, zc) = (0, 0)
		while n != 0:
			if n & 1 == 0:
				zc += 1
			else:
				oc += 1
			n = n >> 1
		if oc == 1 and zc % 3 == 0:
			return 'Yes'
		return 'No'


class Solution:

	def is_power_of_eight(self, n):
		ans = 1
		while ans <= n:
			if ans == n:
				return 'Yes'
			ans = ans * 8
		return 'No'


class Solution:

	def is_power_of_eight(self, n):
		if 8 | n == 8 and n >= 8:
			return 'Yes'
		elif n & n - 1 == 0:
			return 'Yes'
		else:
			return 'No'


class Solution:

	def is_power_of_eight(self, n):
		(set, pos, i) = (0, -1, 0)
		while n is not 0:
			if n & 1:
				pos = i
			set += n & 1
			n >>= 1
			i += 1
		if set > 1:
			return 'No'
		if pos % 3 == 0:
			return 'Yes'
		return 'No'


class Solution:

	def is_power_of_eight(self, n):
		for i in range(64):
			if pow(8, i) == n:
				return 'Yes'
		return 'No'


class Solution:

	def is_power_of_eight(self, n):
		while n:
			if n == 8:
				return 'Yes'
			if n % 8 != 0:
				return 'No'
			n = n // 8
		return 'No'


import math

class Solution:

	def is_power_of_eight(self, n):
		k = int(math.log(n, 8))
		if 8 ** k == n:
			return 'Yes'
		else:
			return 'No'


class Solution:

	def is_power_of_eight(self, n):
		import math
		x = math.log(n, 8)
		if 8 ** int(x) == n:
			return 'Yes'
		return 'No'


import math

class Solution:

	def is_power_of_eight(self, n):
		i = math.log(n) / math.log(8)
		if i - math.floor(i) < 1e-05:
			return 'Yes'
		else:
			return 'No'


class Solution:

	def is_power_of_eight(self, n):
		l = 1
		while l <= n:
			if l & n == n:
				return 'Yes'
			l <<= 3
		return 'No'


class Solution:

	def is_power_of_eight(self, n):
		if n & n - 1 or n == 0:
			return 'No'
		c = -1
		while n:
			n >>= 1
			c += 1
		if c % 3 == 0:
			return 'Yes'


class Solution:

	def is_power_of_eight(self, n):
		d = n
		if n == 1:
			return 'Yes'
		elif n % 8 != 0:
			return 'No'
		if n % 8 == 0:
			while d >= 1:
				d = d / 8
				if d == 1:
					return 'Yes'
				elif d % 8 != 0:
					return 'No'


import math

class Solution:

	def is_power_of_eight(self, n):
		k = int(math.log2(n))
		if 2 ** k == n and k % 3 == 0:
			return 'Yes'
		return 'No'


from math import log2

class Solution:

	def is_power_of_eight(self, n):
		p = log2(n) // 3
		return ['No', 'Yes'][pow(8, p) == n]


class Solution:

	def is_power_of_eight(self, n):
		import math
		c = round(math.log(n) / math.log(8))
		if n == pow(8, c):
			return 'Yes'
		else:
			return 'No'


from math import *

class Solution:

	def is_power_of_eight(self, n):
		a = log(n)
		b = log(8)
		res1 = round(a // b, 3)
		res2 = round(a / b, 3)
		return 'Yes' if res1 == res2 else 'No'


class Solution:

	def is_power_of_eight(self, n):
		for x in range(1, 200):
			if n == 8 ** x:
				return 'Yes'
			elif 8 ** x > n:
				return 'No'


class Solution:

	def is_power_of_eight(self, n):
		if n == 0:
			return 'No'
		if n & n - 1 == 0:
			return 'Yes'
		else:
			return 'No'


class Solution:

	def is_power_of_eight(self, n):
		for i in range(0, 65):
			if pow(8, i) == n:
				return 'Yes'
		return 'No'
if __name__ == '__main__':
	T = int(input())
	for i in range(T):
		n = int(input())
		ob = Solution()
		ans = ob.is_power_of_eight(n)
		print(ans)


class Solution:

	def is_power_of_eight(self, n):
		i = n
		while i != 1:
			if i % 8 != 0:
				return 'No'
			else:
				i = i // 8
		return 'Yes'
if __name__ == '__main__':
	T = int(input())
	for i in range(T):
		n = int(input())
		ob = Solution()
		ans = ob.is_power_of_eight(n)
		print(ans)


class Solution:

	def is_power_of_eight(self, n):
		return 'Yes' if n == 1 or (n % 8 == 0 and bin(n).count('1') == 1) else 'No'


import math

class Solution:

	def is_power_of_eight(self, n):
		d = math.log(n, 8)
		d = round(d, 5)
		if int(d) == d:
			return 'Yes'
		else:
			return 'No'


import math

class Solution:

	def is_power_of_eight(self, n):
		countOne = countZero = 0
		N = n
		while n:
			if n & 1:
				countOne += 1
			else:
				countZero += 1
			n = n >> 1
		result = self.checkMSBisOne(N) and countOne == 1 and (countZero % 3 == 0)
		return 'Yes' if result else 'No'

	def checkMSBisOne(self, n):
		siz = self.getLength(n)
		return n >> siz - 1 & 1 == 1

	def getLength(self, n):
		return int(math.log2(n)) + 1


class Solution:

	def is_power_of_eight(self, n):
		x = n
		if x and x & x - 1 == 0:
			while x > 0:
				x = x >> 3
				if x & 1:
					return 'Yes'
		return 'No'


class Solution:

	def is_power_of_eight(self, n):
		count = 0
		a = 0
		while n != 0:
			if n & 1 == 0:
				count += 1
			else:
				a += 1
			n = n >> 1
		if a == 1 and count % 3 == 0:
			return 'Yes'
		return 'No'


class Solution:

	def is_power_of_eight(self, n):
		b = bin(n)[2:]
		z = b.count('8')
		o = b.count('1')
		if z % 3 == 0 and o == 1:
			return 'Yes'
		return 'No'


class Solution:

	def is_power_of_eight(self, n):
		if n and (not n & n - 1) and (not n & 3067833782):
			return 'Yes'
		else:
			return 'No'


class Solution:

	def is_power_of_eight(self, n):
		a = 0
		b = 8 ** a
		while b < n:
			a = a + 1
			b = 8 ** a
		if b == n:
			return 'Yes'
		return 'No'


class Solution:

	def is_power_of_eight(self, n):
		a = 0
		b = 2 ** (3 * a)
		while b < n:
			a = a + 1
			b = 2 ** (3 * a)
		if b == n:
			return 'Yes'
		return 'No'


import math

class Solution:

	def is_power_of_eight(self, n):
		while n > 1:
			if n % 8 != 0:
				return 'No'
			n //= 8
		if n == 1:
			return 'Yes'