Given a number N. Check whether it is a magic number or not.
Note:- A number is said to be a magic number, if the sum of its digits are calculated till a single digit recursively by adding the sum of the digits after every addition and the single digit comes out to be 1.
Example 1:
Input: N = 1234
Output: 1
Explanation: 1 + 2 + 3 + 4 = 10, 
Again 1 + 0 = 1. Thus, 1234 is a magic 
number. 
Example 2:
Input: N = 1235
Output: 0
Explanation: 1 + 2 + 3 + 5 = 11. Again, 
1 + 1 = 2. Thus, 1235 is not a magic number.
Your Task:
You don't need to read input or print anything.Your task is to complete the function isMagic() which takes a number N as input parameter and returns 1 if the number is a magic number.Otherwise, it returns 0.
Expected Time Complexity:O(LogN)
Expected Auxillary Space:O(1)
Constraints:
1<=N<=10^{9}

Source: geeksforgeeks | Difficulty: EASY
Tags: Mathematics

Starter code: #User function Template for python3



class Solution:

    def isMagic(self,N):

        #code here

Solutions:
class Solution:

	def isMagic(self, N):
		if N < 9:
			if N == 1:
				return 1
			else:
				return 0
		while N >= 10:
			summ = 0
			while N > 0:
				rem = N % 10
				summ += rem
				N = N // 10
			N = summ
		if summ == 1:
			return 1
		return 0


class Solution:

	def isMagic(self, N):
		s = str(N)
		while len(s) > 1:
			d = [int(x) for x in s]
			d = sum(d)
			s = str(d)
		if s == '1':
			return 1
		else:
			return 0


class Solution:

	def isMagic(self, N):
		if N % 9 == 1:
			return 1
		else:
			return 0


class Solution:

	def isMagic(self, N):
		if N == 1:
			return 1
		s = 0
		a = N
		while a > 9:
			s = 0
			while N != 0:
				d = N % 10
				s = s + d
				N = N // 10
			if s > 9:
				a = s
				N = a
			elif s == 1:
				return 1
			else:
				return 0
		return 0


class Solution:

	def isMagic(self, N):

		def helper(x):
			ans = 0
			while x != 0:
				ans = ans + x % 10
				x //= 10
			return ans
		while N > 9:
			N = helper(N)
		if N == 1:
			return 1
		return 0


class Solution:

	def isMagic(self, N):

		def helper(x):
			m = str(x)
			if len(m) == 1:
				return x
			ans = 0
			for i in m:
				ans += int(i)
			return ans
		while N > 9:
			N = helper(N)
		if N == 1:
			return 1
		return 0


class Solution:

	def isMagic(self, N):
		s = str(N)
		a = [*s]
		m = map(int, a)
		b = sum(m)
		x = str(b)
		y = [*x]
		z = map(int, y)
		mg = sum(z)
		mgl = [1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000]
		if mg in mgl:
			return 1
		else:
			return 0


class Solution:

	def isMagic(self, N: int) -> int:
		if self.repeatedDigitsSum(N) == 1:
			return 1
		else:
			return 0

	@staticmethod
	def sumOfDigits(num: int) -> int:
		if not num:
			return 0
		else:
			return num % 10 + Solution.sumOfDigits(num // 10)

	@staticmethod
	def repeatedDigitsSum(n: int) -> int:
		if n < 10:
			return n
		else:
			return Solution.repeatedDigitsSum(Solution.sumOfDigits(n))


class Solution:

	def isMagic(self, N):
		get1 = '1'
		get2 = '0'
		num = 0
		arr = list(str(N))
		for get in range(len(arr)):
			num += int(arr[get])
		count = 0
		query = list(str(num))
		for check in range(len(query)):
			count += int(query[check])
		ll = list(str(count))
		if max(ll) == '1' and ll.count('1') == 1:
			return get1
		else:
			return get2


class Solution:

	def isMagic(self, n):
		s = 0
		while n > 0 or s > 9:
			if n == 0:
				n = s
				s = 0
			s += n % 10
			n = n // 10
		if s == 1:
			return 1
		else:
			return 0


class Solution:

	def isMagic(self, N):
		for i in range(N):
			if N == 1:
				return 1
			elif N < 10:
				return 0
			else:
				sum = 0
				for i in str(N):
					sum = sum + int(i)
				N = sum
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isMagic(N))


class Solution:

	def isMagic(self, N):
		if N == 1:
			return 1
		elif N >= 0 and N <= 9:
			return 0
		else:
			total = 0
			while N > 0:
				total = total + N % 10
				N = N // 10
			return self.isMagic(total)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isMagic(N))


class Solution:

	def isMagic(self, n):
		if n % 9 == 1:
			return 1
		else:
			return 0
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isMagic(N))


class Solution:

	def isMagic(self, N):
		while N >= 10:
			b = str(N)
			s = 0
			for i in b:
				s += int(i)
			N = s
		if N == 1:
			return 1
		return 0
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isMagic(N))


class Solution:

	def isMagic(self, N):
		num = 0
		if N == 1:
			return 1
		elif N > 1 and N < 10:
			return 0
		else:
			while N != 0:
				value = N % 10
				num = num + value
				N //= 10
			if num == 1:
				return 1
			elif num > 9:
				N = num
				return self.isMagic(N)
			else:
				return 0


class Solution:

	def digit_sum(self, n):
		w = 0
		r = 1
		q = 0
		while n != 0:
			q = n % 10
			w = q + w
			n = n // 10
		return w

	def isMagic(self, N):
		q = 0
		while N >= 10:
			N = self.digit_sum(N)
		if N == 1:
			return 1
		return 0


class Solution:

	def isMagic(self, N):
		s = 0
		while N > 0:
			s += N % 10
			N = N // 10
		while s // 10 != 0:
			sn = 0
			while s > 0:
				sn += s % 10
				s = s // 10
			s = sn
		if s == 1:
			return 1
		return 0


class Solution:

	def isMagic(self, n):
		summ = 0
		if n == 1:
			return 1
		elif n < 10 and n != 1:
			return 0
		else:
			num = str(n)
			for i in num:
				summ += int(i)
			if summ == 1:
				return 1
			elif summ > 9:
				n = summ
				return self.isMagic(n)
			else:
				return 0


class Solution:

	def add_num(self, N):
		s1 = 0
		while N > 0:
			s1 += N % 10
			N = N // 10
		if s1 < 10:
			return s1
		else:
			return self.add_num(s1)

	def isMagic(self, N):
		ans = self.add_num(N)
		if ans == 1:
			return 1
		else:
			return 0


class Solution:

	def isMagic(self, N):
		if N == 1:
			return 1
		if len(str(N)) == 1 and N != 1:
			return 0
		summ = 0
		while N >= 10:
			remainder = N % 10
			summ += remainder
			N = N // 10
		return self.isMagic(summ + N)


class Solution:

	def isMagic(self, N):
		sum = 0
		m = N
		while m > 0:
			sum += m % 10
			m = m // 10
		if sum == 1:
			return 1
		else:
			while sum > 0:
				k = sum
				sum = 0
				while k > 0:
					sum += k % 10
					k = k // 10
				if sum == 1:
					return 1
				elif sum // 10 == 0:
					break
			return 0