Your task is to calculate sum  of primes present as digits of given number N.
Example 1:
Input: 333
Output: 9
Explaination: 3 is a prime number. It 
is present 3 times. So 3+3+3 = 9.
Example 2:
Input: 686
Output: 0
Explaination: Neither 6 nor 8 is a 
prime number.
Your Task:
You do not need to read input or print anything. Your task is to complete the function primeSum() which takes N as input parameter and returns the sum of all the prime digits present in the number N.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 5*10^{4}

Source: geeksforgeeks | Difficulty: EASY
Tags: Number theory

Mathematics

Starter code: #User function Template for python3

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

Solutions:
import math

class Solution:

	def primeSum(self, N):

		def prime(n):
			if n <= 1:
				return False
			for i in range(2, int(math.sqrt(n) + 1)):
				if n % i == 0:
					return False
			return True
		c = 0
		m = []
		while N > 0:
			d = N % 10
			m.append(d)
			N = N // 10
		for i in m:
			if prime(i):
				c += i
		return c


class Solution:

	def primeSum(self, N):
		s = 0
		N = str(N)
		for i in N[::1]:
			i = int(i)
			if i == 2 or i == 3 or i == 5 or (i == 7):
				s += i
		return s


class Solution:

	def primeSum(self, N):

		def prime(n):
			if n == 0 or n == 1:
				return 0
			for i in range(2, int(n // 2) + 1):
				if n % i == 0:
					return 0
			return 1
		count = 0
		for i in str(N):
			if prime(int(i)) == 1:
				count += int(i)
		return count


class Solution:

	def primeSum(self, N):
		n = str(N)
		s = 0
		for i in n:
			count = 0
			m = int(i)
			for j in range(1, m + 1):
				if m % j == 0:
					count += 1
			if count == 2:
				s += m
		return s


import math

class Solution:

	def primeSum(self, N):

		def isPrime(n):
			if n == 1:
				return 0
			for i in range(2, int(math.sqrt(n) + 1)):
				if n % i == 0:
					return 0
			return 1
		a = set()
		val = 0
		for i in str(N):
			if int(i) in a:
				val += int(i)
			elif isPrime(int(i)) == 1:
				a.add(int(i))
				val += int(i)
		return val


class Solution:

	def primeSum(self, N):
		sum = 0
		while N != 0:
			a = N % 10
			N = N // 10
			if a == 2 or a == 3 or a == 5 or (a == 7):
				sum = sum + a
		return sum


class Solution:

	def primeSum(self, N):

		def prime(n):
			if n <= 1:
				return False
			for i in range(2, n):
				if n % i == 0:
					return False
			return True
		s = 0
		while N:
			r = N % 10
			if prime(r):
				s = s + r
			N = N // 10
		return s


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 primeSum(self, N):
		s = str(N)
		c = 0
		for i in s:
			if fun(int(i)):
				c += int(i)
		return c


class Solution:

	def primeSum(self, n):
		su = 0
		s = str(n)
		for i in s:
			i = int(i)
			c = 0
			for j in range(1, i):
				if i % j == 0:
					c += 1
			if c == 1:
				su += i
		return su


class Solution:

	def primeSum(self, N):

		def is_prime(n):
			import math
			a = int(math.sqrt(n))
			if n < 2:
				return False
			for i in range(2, a + 1):
				if n % i == 0:
					return False
			return True
		s = 0
		l = []
		while N:
			d = N % 10
			N = N // 10
			if is_prime(d) == True:
				l.append(d)
		return sum(l)


class Solution:

	def primeSum(self, N):
		x = str(N)
		k = 0
		l = []
		for i in x:
			c = 0
			h = int(i)
			if h == 1:
				continue
			for j in range(2, h):
				if h % j == 0:
					c = 1
					break
			if c == 0:
				l.append(i)
		for i in l:
			k += int(i)
		return k


class Solution:

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

	def primeSum(self, N):
		N = list(str(N))
		summ = 0
		for i in N:
			if self.isprime(int(i)):
				summ += int(i)
		return summ


class Solution:

	def primeSum(self, N):
		import math

		def is_prime(n):
			if n < 2:
				return False
			s = int(math.sqrt(n))
			for i in range(2, s + 1):
				if n % i == 0:
					return False
			return True
		s = str(N)
		l = list(map(int, s))
		a = []
		for i in l:
			if is_prime(i) == True:
				a.append(i)
		return sum(a)


from math import sqrt

class Solution:

	def primeSum(self, N):
		sumOfPrime = 0
		while N:
			digit = N % 10
			prime = self.isPrime(digit)
			if prime:
				sumOfPrime += digit
			N //= 10
		return sumOfPrime

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


import math

class Solution:

	def primeSum(self, N):

		def isprime(num):
			if num < 2:
				return False
			for i in range(2, int(math.sqrt(num)) + 1):
				if num % i == 0:
					return False
			return True
		primesum = 0
		for digit in str(N):
			if isprime(int(digit)):
				primesum += int(digit)
		return primesum


class Solution:

	def primeSum(self, N):
		s = 0
		while N > 0:
			r = N % 10
			p = 0
			if r <= 1:
				p = 1
			for i in range(2, int(r ** 0.5) + 1):
				if r % i == 0:
					p += 1
			if p == 0:
				s += r
			N = N // 10
		return s


class Solution:

	def primeSum(self, n):
		import math

		def prime(n):
			if n == 1 or n == 0:
				return False
			k = int(math.sqrt(n))
			for i in range(2, k + 1):
				if n % i == 0:
					return False
			return True
		sum = 0
		while n:
			d = n % 10
			if prime(d):
				sum += d
			n = n // 10
		return sum


import math

class Solution:

	def primeSum(self, N):

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


import math

class Solution:

	def primeSum(self, N):
		s = str(N)
		v = 0
		for i in s:
			n = int(i)
			c = 0
			for i in range(2, int(math.sqrt(n) + 1)):
				if n % i == 0:
					c += 1
			if c == 0 and n != 1:
				v = v + n
		return v


class Solution:

	def primeSum(self, N):
		a = []
		sum = 0
		while N != 0:
			c = N % 10
			N = N // 10
			if c <= 1:
				continue
			f = 0
			for i in range(2, int(c ** 0.5) + 1):
				if c % i == 0:
					f = 1
					break
			if f == 0:
				sum = sum + c
		return sum


class Solution:

	def primeSum(self, N):
		a = []
		b = []
		l = str(N)
		for i in l:
			if int(i) > 1:
				for j in range(2, int(int(i) ** 0.5) + 1):
					if int(i) % int(j) == 0:
						break
				else:
					a.append(i)
		lst = list(map(int, a))
		return sum(lst)


class Solution:

	def primeSum(self, N):
		a = str(N)
		b = []
		for i in a:
			if int(i) > 1:
				for j in range(2, int(int(i) ** 0.5) + 1):
					if int(i) % j == 0:
						break
				else:
					b.append(int(i))
		if len(b) == 0:
			return 0
		else:
			return sum(b)


import math

class Solution:

	def isPrime(self, N):
		if N <= 1:
			return 0
		for i in range(2, int(math.sqrt(N) + 1)):
			if N % i == 0:
				return 0
			i += 1
		return 1

	def primeSum(self, N):
		N = list(str(N))
		sum = 0
		for i in N:
			if self.isPrime(int(i)):
				sum += int(i)
		return sum


class Solution:

	def primeSum(self, N):
		prime_sum = 0
		for digit in str(N):
			num = int(digit)
			if self.isPrime(num):
				prime_sum += num
		return prime_sum

	def isPrime(self, num):
		if num < 2:
			return False
		if num == 2 or num == 3:
			return True
		if num % 2 == 0 or num % 3 == 0:
			return False
		for i in range(3, int(num ** 0.5) + 1, 2):
			if num % i == 0:
				return Fals
		return True


import math

class Solution:

	def primeSum(self, N):
		s = int(math.sqrt(N))
		for i in range(2, s + 1):
			if i < 2:
				return 0
		s = 0
		for i in str(N):
			if i == '2' or i == '3' or i == '5' or (i == '7'):
				s += int(i)
		return s
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()


import math

class Solution:

	def primeSum(self, N):
		s = int(math.sqrt(N))
		for i in range(2, s + 1):
			if i < 2:
				return 0
		s = 0
		for i in str(N):
			if i == '2' or i == '3' or i == '5' or (i == '7'):
				s += int(i)
		return s


class Solution:

	def primeSum(self, N):
		sum = 0

		def prime(n):
			if n < 2:
				return False
			for i in range(2, int(n ** 0.5) + 1):
				if n % i == 0:
					return False
			return True
		while N:
			d = N % 10
			if prime(d):
				sum = sum + d
			N = N // 10
		return sum


class Solution:

	def primeSum(self, N):
		l = []
		while N != 0:
			r = N % 10
			l.append(r)
			N = N // 10
		k = []
		for i in l:
			c = 0
			for j in range(1, i):
				if i % j == 0:
					c = c + 1
			if c == 1:
				k.append(i)
		return sum(k)


class Solution:

	def primeSum(self, N):
		c = 0
		for i in str(N):
			s = int(i)
			if s == 2:
				c += 2
			elif s % 2 == 0 or s == 1:
				continue
			else:
				for i in range(2, int(s ** 0.5) + 1):
					if s % i == 0:
						break
				else:
					c += s
		return c


import math

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

class Solution:

	def primeSum(self, N):
		c = 0
		lst = []
		while N:
			x = N % 10
			N = N // 10
			lst.append(x)
		for i in lst:
			if is_prime(i) == True:
				c += i
		return c


class Solution:

	def primeSum(self, R):
		import math
		sm = 0

		def is_prime(N):
			if N < 2:
				return False
			s = int(math.sqrt(N))
			for i in range(2, s + 1):
				if N % i == 0:
					return False
			return True
		while R:
			rem = R % 10
			R = R // 10
			if is_prime(rem):
				sm += rem
		return sm


class Solution:

	def primeSum(self, N):

		def is_prime(num):
			if num < 2:
				return False
			for i in range(2, int(num ** 0.5) + 1):
				if num % i == 0:
					return False
			return True
		digits = str(N)
		prime_sum = 0
		for digit in digits:
			if is_prime(int(digit)):
				prime_sum += int(digit)
		return prime_sum


import math

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

class Solution:

	def primeSum(self, N):
		a = list(str(N))
		v = list(map(int, a))
		s = 0
		for i in v:
			if isprime(i):
				s += i
		return s


import math

class Solution:

	def isprime(self, N):
		if N <= 1:
			return False
		i = 2
		while i * i <= N:
			if N % i == 0:
				return False
			i += 1
		return True

	def primeSum(self, N):
		N = list(str(N))
		s = 0
		for i in N:
			if self.isprime(int(i)):
				s += int(i)
		return s


import math

class Solution:

	def primeSum(self, N):
		sumo = 0
		while N != 0:
			d = N % 10
			c = 0
			if d <= 1:
				c = 1
			s = int(math.sqrt(d))
			for i in range(2, s + 1):
				if d % i == 0:
					c = 1
			if c == 0:
				sumo += d
			N = N // 10
		return sumo


import math

class Solution:

	def primeSum(self, N):

		def isPrime(N):
			c = 0
			if N < 2:
				return False
			s = int(math.sqrt(N))
			for i in range(2, s + 1):
				if N % i == 0:
					c = c + 1
			if c == 0:
				return 1
			else:
				return 0
		s = str(N)
		k = []
		c = 0
		for i in s:
			k.append(int(i))
		for j in k:
			if isPrime(j) == 1:
				c = c + j
		return c


import math

class Solution:

	def primeSum(self, N):
		p = str(N)
		l = list(p)
		s = 0
		for i in l:
			k = int(i)
			if k < 2:
				continue
			e = int(math.sqrt(k))
			for j in range(2, e + 1):
				if k % j == 0:
					break
			else:
				s += k
		return s


class Solution:

	def primeSum(self, n):
		s = 0

		def prime(x):
			fc = 0
			for i in range(1, x + 1):
				if x % i == 0:
					fc += 1
			if fc == 2:
				return True
			else:
				return False
		while n:
			r = n % 10
			if prime(r):
				s += r
			n = n // 10
		return s


class Solution:

	def isPrime(self, N):
		if N <= 1:
			return False
		i = 2
		while i * i <= N:
			if N % i == 0:
				return False
			i += 1
		return True

	def primeSum(self, N):
		N = list(str(N))
		s = 0
		for i in N:
			if self.isPrime(int(i)):
				s += int(i)
		return s


class Solution:

	def primeSum(self, N):
		sp = 0
		for i in str(N):
			if i == '1' or i == '0':
				pass
			else:
				s = int(int(i) ** 0.5)
				count = 0
				for j in range(2, s + 1):
					if int(i) % j == 0:
						count += 1
				if count == 0:
					sp = sp + int(i)
		return sp


import math as m

class Solution:

	def primeSum(self, n):
		n = str(n)
		n = list(n)
		n = [eval(i) for i in n]
		sm = 0
		for i in n:
			c = 0
			b = int(m.sqrt(i))
			if i == 1:
				continue
			for j in range(1, b + 1):
				if i % j == 0:
					c += 1
			if c == 1:
				sm = sm + i
			c = 0
		return sm


class Solution:

	def primeSum(self, n):
		e = []
		while n > 0:
			r = n % 10
			c = 0
			if r == 1:
				c += 1
			for i in range(2, int(r ** 0.5) + 1):
				if r % i == 0:
					c = 1
					break
			if c > 0:
				pass
			else:
				e.append(r)
			n = n // 10
		return sum(e)


import math

class Solution:

	def primeSum(self, N):
		x = 0
		while N != 0:
			r = N % 10
			N = N // 10
			c = 0
			if r == 1:
				c += 1
			for i in range(2, int(math.sqrt(r)) + 1):
				if r % i == 0:
					c += 1
					break
			if c == 0:
				x += r
		return x


import math

class Solution:

	def primeSum(self, N):

		def isPrime(N):
			f = 0
			if N == 1:
				return 0
			for i in range(2, int(math.sqrt(N) + 1)):
				if N % i == 0:
					return 0
			else:
				return 1
		s = 0
		while N > 0:
			r = N % 10
			if isPrime(r):
				s += r
			N = N // 10
		return s


import math

class Solution:

	def primeSum(self, N):

		def isPrime(N):
			if N == 1:
				return False
			s = int(math.sqrt(N))
			for i in range(2, s + 1):
				if N % i == 0:
					return False
			return True
		sum = 0
		while N > 0:
			r = N % 10
			if isPrime(r):
				sum += r
			N = N // 10
		return sum


import math

class Solution:

	def primeSum(self, N):
		s = str(N)
		d = 0
		n = list(map(int, s))
		for i in n:
			c = True
			if i < 2:
				c = False
			else:
				for j in range(2, int(math.sqrt(i)) + 1):
					if i % j == 0:
						c = False
			if c != False:
				d += i
		return d


import math

class Solution:

	def primeSum(self, N):
		s = 0
		l = {2, 3, 5, 7}
		while N > 0:
			r = N % 10
			if r in l:
				s += r
			N = N // 10
		return s