Given a number N, you need to write a program that finds the sum of digits of the number till the number becomes a single digit and then check whether the number is Prime/Non-Prime.
Example 1:
Input:
N=5602
Output:
0
Explanation:
1st step=5+6+0+2=13
2nd step=1+3=4
Since 4 is not prime, so answer is 0.
Example 2:
Input:
N=12
Output:
1
Explanation:
1st step=1+2=3
Since, 3 is prime, so answer =1.
Your Task:
You don't need to read input or print anything.Your Task is to complete the function digitPrime() which takes a number N as input parameter and returns 1 if the final digit sum(adding digits until it becomes single-digit number) is prime.Otherwise, it returns 0.
Expected Time Complexity:O(1)
Expected Auxillary Space:O(1)
Constraints:
1<=N<=10^{9}

Source: geeksforgeeks | Difficulty: EASY
Tags: Number theory

Starter code: #User function template for Python

class Solution:
    def digitsPrime(self,N):
        #code here

Solutions:
class Solution:

	def digitsPrime(self, N):
		m = str(N)
		while len(m) != 1:
			s = 0
			for i in m:
				s += int(i)
			m = str(s)
		list1 = [2, 3, 5, 7]
		if int(m) in list1:
			return 1
		return 0


import math

def fun(n):
	if n == 0 or n == 1:
		return False
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			return False
	return True

class Solution:

	def digitsPrime(self, N):
		s = N % 9
		if fun(s):
			return 1
		return 0


class Solution:

	def digitsPrime(self, N):
		l = []
		s = N % 9
		if len(str(s)) > 1:
			digitsPrime(s)
		for i in range(1, s + 1):
			if s % i == 0:
				l = l + [i]
		if len(l) == 2:
			return 1
		else:
			return 0


class Solution:

	def digitsPrime(self, N):
		n = N % 9
		if n > 1:
			for i in range(2, int(n / 2) + 1):
				if n % i == 0:
					return 0
			else:
				return 1
		else:
			return 0


class Solution:

	def isPrime(self, n):
		if n < 2:
			return 0
		for x in range(2, int(n ** 0.5) + 1):
			if not n % x:
				return 0
		return 1

	def digitsPrime(self, N):
		while True:
			N = sum(map(int, str(N)))
			if N < 10:
				break
		return self.isPrime(N)


import math

def prime(n):
	if n < 2:
		return False
	c = 0
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			c = c + 1
	if c == 0:
		return True
	return False

class Solution:

	def digitsPrime(self, N):
		c = 0
		while N > 0:
			r = N % 10
			c = c + r
			N = N // 10
			if c > 9 and N == 0:
				N = c
				c = 0
		if prime(c) == True:
			return 1
		return 0


class Solution:

	def digitsPrime(self, N):

		def helper(x):
			n = str(x)
			if len(n) <= 1:
				return x
			ans = 0
			for i in n:
				ans += int(i)
			return ans
		while len(str(N)) != 1:
			N = helper(N)
		if N == 2:
			return 1
		if N == 1:
			return 0
		for i in range(2, int(N ** 0.5) + 1):
			if N % i == 0:
				return 0
		return 1


class Solution:

	def digitsPrime(self, N):
		from math import sqrt
		s = N

		def prime(n):
			if n == 1:
				return 0
			if n == 2:
				return 1
			if n % 2 == 0:
				return 0
			for i in range(3, int(sqrt(n)) + 1, 2):
				if n % i == 0:
					return 0
			return 1

		def che(n):
			k = 0
			while n:
				d = n % 10
				k = k + d
				n = n // 10
			return k
		while s > 9:
			s = che(s)
		if prime(s) == 1:
			return 1
		return 0


class Solution:

	def digitsPrime(self, n):

		def fun(n):
			k = 0
			while n != 0:
				d = n % 10
				k = k + d
				n = n // 10
			return k
		if n == 1:
			return 0
		while n >= 10:
			k = fun(n)
			n = k
		if k == 2 or k == 3 or k == 5 or (k == 7):
			return 1
		return 0


class Solution:

	def digitsPrime(self, N):
		while True:
			N = sum((int(x) for x in str(N)))
			if N < 10:
				break
		result = N in {2, 3, 5, 7}
		return 1 if result else 0


class Solution:

	def digitsPrime(self, N):

		def p(a):
			if a == 0 or a == 1:
				return False
			s = int(a ** 0.5)
			for i in range(2, s + 1):
				if a % i == 0:
					return False
			return True
		s = N % 9
		if p(s):
			return 1
		return 0


class Solution:

	def digitsPrime(self, N):
		p = '2357'
		if str(N % 9) in p:
			return 1
		return 0


import math as m

class Solution:

	def digitsPrime(self, N):
		a = N % 9
		v = '014689'
		a = str(a)
		if a in v:
			return 0
		else:
			return 1


class Solution:

	def digitsPrime(self, N):

		def isPrime(num):
			if num < 2:
				return 0
			for i in range(2, int(num ** 0.5) + 1):
				if num % i == 0:
					return 0
			return 1

		def digitSum(num):
			if num == 0:
				return 0
			return num % 10 + digitSum(num // 10)
		t = digitSum(N)
		while t >= 10:
			t = digitSum(t)
		if isPrime(t) == 0:
			return 0
		return 1


class Solution:

	def digitsPrime(self, n):
		s = 0
		while 1:
			while n != 0:
				s += n % 10
				n = n // 10
			if s <= 9:
				break
			n = s
			s = 0
		if s == 2:
			return 1
		if s == 1:
			return 0
		for i in range(2, s // 2 + 1):
			if s % i == 0:
				return 0
		return 1


class Solution:

	def digitsPrime(self, N):
		if N == 0:
			return 0
		elif N % 9 in [2, 3, 5, 7]:
			return 1
		return 0
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.digitsPrime(N))


class Solution:

	def digitsPrime(self, N):
		s = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
		while N >= 10:
			b = str(N)
			c = 0
			for i in b:
				c += s[i]
			N = int(c)
		if N == 2 or N == 3 or N == 5 or (N == 7):
			return 1
		return 0
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.digitsPrime(N))


class Solution:

	def digitsPrime(self, n):
		s = 0
		while 1:
			while n != 0:
				s += n % 10
				n = n // 10
			if s <= 9:
				break
			n = s
			s = 0
		if s == 2:
			return 1
		if s == 1:
			return 0
		for i in range(2, s // 2 + 1):
			if s % i == 0:
				return 0
		return 1
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.digitsPrime(N))


class Solution:

	def digitsPrime(self, N):
		if N == 0 or N % 9 == 0:
			return 0
		if N % 9 == 2 or N % 9 == 3 or N % 9 == 5 or (N % 9 == 7):
			return 1
		return 0


import math

class Solution:

	def digitsPrime(self, N):

		def prime(n):
			if n == 0 or n == 1:
				return 0
			for i in range(2, int(math.sqrt(n) + 1)):
				if n % i == 0:
					return 0
			return 1

		def digits(N):
			l = []
			while N > 0:
				l.append(N % 10)
				N = N // 10
			return l
		s = digits(N)
		while len(s) != 1:
			a = sum(s)
			s = digits(a)
		if prime(s[0]) == 1:
			return 1
		else:
			return 0


class Solution:

	def check(self, n):
		if n < 2:
			return False
		count = 2
		while count * count <= n:
			if n % count == 0:
				return False
			count += 1
		return True

	def singleDigit(self, N):
		temp = 0
		for i in str(N):
			temp += int(i)
		if temp > 9:
			return self.singleDigit(temp)
		return temp

	def digitsPrime(self, N):
		digit = self.singleDigit(N)
		check = self.check(digit)
		if check == True:
			return 1
		return 0


class Solution:

	def digitsPrime(self, N):
		s = N % 9
		if N == 0 or N % 9 == 0:
			return 0
		elif s == 2 or s == 3 or s == 5 or (s == 7):
			return 1
		else:
			return 0


from math import *

class Solution:

	def isprime(self, N):
		if N == 1 or N == 0:
			return False
		sq = int(sqrt(N))
		for k in range(2, sq + 1):
			if N % k == 0:
				return False
		return True

	def digitsPrime(self, N):
		while N > 9:
			d = N % 10
			r = N // 10
			c = d + r
			N = c
		if self.isprime(N):
			return 1
		return 0


class Solution:

	def digitsPrime(self, N):
		n = N % 9
		if n == 1 or n == 0:
			return 0
		import math
		for i in range(2, int(math.sqrt(n)) + 1):
			if n % i == 0:
				return 0
		return 1


import math

class Solution:

	def digitsPrime(self, N):
		while True:
			sum1 = 0
			while N:
				sum1 += N % 10
				N = N // 10
			N = sum1
			if sum1 - 9 <= 0:
				break
		return isprime(N)

def isprime(n):
	if n == 1 or n == 0:
		return 0
	if n == 2 or n == 3:
		return 1
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			return 0
	return 1


from math import sqrt

def prime(N):
	if N <= 1:
		return 0
	for i in range(2, int(sqrt(N)) + 1):
		if N % i == 0:
			return 0
	else:
		return 1

class Solution:

	def digitsPrime(self, N):
		sum = 0
		while N > 0 or sum > 9:
			if N == 0:
				N = sum
				sum = 0
			sum += N % 10
			N = N // 10
		if prime(sum) == 1:
			return 1
		else:
			return 0


class Solution:

	def digitsPrime(self, N):
		s = str(N)
		l = len(s)
		sum = 0
		if l == 1:
			sum = int(s)
		while l > 1:
			sum = 0
			for i in s:
				sum += int(i)
			s = str(sum)
			l = len(s)
		prime = True
		if sum <= 1:
			prime = False
		elif sum == 2:
			prime = True
		else:
			for j in range(2, sum):
				if sum % j == 0:
					prime = False
		if prime:
			return 1
		else:
			return 0


class Solution:

	def digitsPrime(self, N):
		while N > 9:
			sum = 0
			while N > 0:
				a = N % 10
				sum = sum + a
				N = N // 10
			N = sum
		if N == 1:
			return 0
		else:
			for i in range(2, N):
				if N % i == 0:
					return 0
					break
			else:
				return 1