Given a number 'N'. The task is to find the N^{th} number whose each digit is a prime number i.e 2, 3, 5, 7. In other words you have to find nth number of this sequence : 2, 3, 5, 7, 22, 23 ,.. and so on.
Example 1:
Input:
N = 10
Output: 33
Explanation:10^{th} number in the sequence of
numbers whose each digit is prime is 33.
Example 2:
Input:
N = 21
Output: 222
Explanation:21^{st} number in the sequence of
numbers whose each digit is prime is 222.
Your Task:
Complete primeDigits function and return the nth number of the given sequence made of prime digits.
Constraints:
1 <= N <= 100

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Mathematics

Starter code: #User function Template for python3

class Solution:
    
    #Function to find nth number made of only prime digits.
    def nthprimedigitsnumber(self,n):
        # code here


Solutions:
class Solution:

	def nthprimedigitsnumber(self, n):
		t = [2, 3, 5, 7]
		k = 0
		z = 0
		while k <= n:
			count = 0
			for i in str(z):
				if int(i) in t:
					pass
				else:
					count = 1
					break
			if count == 0:
				k = k + 1
			if k == n:
				return z
			z += 1


class Solution:

	def nthprimedigitsnumber(self, n):
		s = ''
		while n > 0:
			k = n % 4
			if k == 1:
				s += '2'
			elif k == 2:
				s += '3'
			elif k == 3:
				s += '5'
			else:
				s += '7'
			n -= 1
			n = n // 4
		return int(s[::-1])


class Solution:

	def nthprimedigitsnumber(self, n):
		K = []
		for i in range(1, 5000):
			tod = list(str(i))
			if '0' not in tod and '1' not in tod and ('4' not in tod) and ('6' not in tod) and ('8' not in tod) and ('9' not in tod):
				K.append(tod)
		ans = K[n - 1]
		finans = ''
		for i in ans:
			finans += i
		return finans


from math import sqrt

class Solution:

	def nthprimedigitsnumber(self, n):
		c = 0
		i = 2
		while True:
			if self.is_digit_prime(i):
				c += 1
				if c == n:
					break
			i += 1
		return i

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

	def is_digit_prime(self, n):
		for i in str(n):
			if not self.is_prime(int(i)):
				return False
		return True


from collections import deque

class Solution:

	def nthprimedigitsnumber(self, n):
		l = {2, 3, 5, 7}
		q = deque()
		c = 0
		q.append(0)
		while len(q) != 0:
			curr = q[0]
			q.popleft()
			for i in l:
				q.append(curr * 10 + i)
				c += 1
				if c == n:
					return curr * 10 + i


class Solution:

	def isprime(self, d):
		if d == 2 or d == 3 or d == 5 or (d == 7):
			return True
		return False

	def isdigprime(self, n):
		for i in str(n):
			if self.isprime(int(i)) == False:
				return False
		return True

	def nthprimedigitsnumber(self, n):
		i = 2
		count = 0
		while True:
			if self.isdigprime(i) == True:
				count = count + 1
			if count == n:
				return i
			i = i + 1


class Solution:

	def nthprimedigitsnumber(self, n):
		if n == 1:
			return 2
		N = 1
		s = {'2', '3', '5', '7'}
		i = 2
		while True:
			i_s = set(str(i))
			if i_s.issubset(s):
				if N == n:
					return i
				N += 1
			i += 1


arr = [2, 3]

class Solution:

	def nthprimedigitsnumber(self, n):
		digits = set('2357')
		i = arr[-1] + 1
		while len(arr) < n:
			flag = True
			for item in str(i):
				if not item in digits:
					flag = False
					break
			if flag:
				arr.append(i)
			i += 1
		return arr[n - 1]


class Solution:

	def nthprimedigitsnumber(self, n):
		l = [2, 3, 5, 7]
		a = [2, 3, 5, 7]
		if n <= 4:
			return l[n - 1]
		else:
			j = 1
			(s, e) = (4 ** (j - 1), 4 ** j)
			while 1:
				for i in range(s - 1, e):
					for z in range(0, 4):
						if len(a) == n:
							return a[-1]
						a.append(a[i] * 10 + l[z])
					if len(a) == n:
						return a[-1]
				s = e + 1
				e = e * 4


class Solution:

	def nthprimedigitsnumber(self, n):
		count = 0
		i = 2
		prime = ['2', '3', '5', '7']
		while True:
			y = 1
			for j in str(i):
				if j not in prime:
					y = -1
					break
			if y == 1:
				count += 1
			y = 1
			i += 1
			if count == n:
				return i - 1


class Solution:

	def nthprimedigitsnumber(self, number):
		num = ''
		while number > 0:
			rem = number % 4
			if rem == 1:
				num += '2'
			if rem == 2:
				num += '3'
			if rem == 3:
				num += '5'
			if rem == 0:
				num += '7'
				number = number - 1
			number = number // 4
		return num[::-1]


class Solution:

	def nthprimedigitsnumber(self, n):

		def checkprime(i):
			if i < 2:
				return 0
			for j in range(2, i // 2 + 1):
				if i % j == 0:
					return 0
			return 1
		x = 2
		count = 0
		while count != n:
			temp = x
			f = 0
			while temp != 0:
				rem = temp % 10
				if checkprime(rem) == 0:
					f = 1
					break
				temp = temp // 10
			if f == 0:
				count += 1
			x += 1
		return x - 1


class Solution:

	def nthprimedigitsnumber(self, n):
		ans = ''
		while n > 0:
			if n % 4 == 1:
				ans += '2'
				n //= 4
			elif n % 4 == 2:
				ans += '3'
				n //= 4
			elif n % 4 == 3:
				ans += '5'
				n //= 4
			elif n % 4 == 0:
				ans += '7'
				n //= 4
				n -= 1
		ans = reversed(ans)
		return ''.join(ans)


class Solution:

	def nthprimedigitsnumber(self, n):
		res = ''
		while n > 0:
			if n % 4 == 1:
				res = '2' + res
			elif n % 4 == 2:
				res = '3' + res
			elif n % 4 == 3:
				res = '5' + res
			elif n % 4 == 0:
				res = '7' + res
			if n % 4 == 0:
				n -= 1
			n //= 4
		return res


class Solution:

	def nthprimedigitsnumber(self, n):
		count = 0
		i = 2
		while True:
			if all((int(digit) in [2, 3, 5, 7] for digit in str(i))):
				count += 1
				if count == n:
					return i
			i += 1


class Solution:

	def nthprimedigitsnumber(self, n):
		d = dict()
		d['2'] = 1
		d['3'] = 1
		d['5'] = 1
		d['7'] = 1
		i = 1
		c = 0
		while True:
			j = 0
			k = 0
			while j < len(str(i)):
				if not str(i)[j] in d:
					k = 1
					break
				j = j + 1
			if k == 0:
				c = c + 1
			if c == n:
				return i
			i = i + 1


class Solution:

	def nthprimedigitsnumber(self, n):
		count = 0
		for i in range(2, 2300):
			if self.allPrimeDigits(i):
				count += 1
			if count == n:
				return i

	def allPrimeDigits(self, n):
		while n > 0:
			r = n % 10
			if r not in [2, 3, 5, 7]:
				return False
			n = n // 10
		return True


class Solution:

	def nthprimedigitsnumber(self, n):
		return self.helper(0, 0, n)

	def helper(self, num, i, n):
		if i > n:
			return -1
		if i == n:
			return num
		return max(self.helper(num * 10 + 2, 4 * i + 1, n), self.helper(num * 10 + 3, 4 * i + 2, n), self.helper(num * 10 + 5, 4 * i + 3, n), self.helper(num * 10 + 7, 4 * i + 4, n))


class Solution:

	def nthprimedigitsnumber(self, n):
		plist = [2, 3, 5, 7]
		for i in [2, 3, 5, 7]:
			for j in [2, 3, 5, 7]:
				plist.append(10 * i + j)
		for i in [2, 3, 5, 7]:
			for j in [2, 3, 5, 7]:
				for k in [2, 3, 5, 7]:
					plist.append(100 * i + 10 * j + k)
		for i in [2, 3, 5, 7]:
			for j in [2, 3, 5, 7]:
				for k in [2, 3, 5, 7]:
					for l in [2, 3, 5, 7]:
						plist.append(1000 * i + 100 * j + 10 * k + l)
		return plist[n - 1]


class Solution:

	def digit(self, i):
		d = {2, 3, 5, 7}
		while i > 0:
			k = i % 10
			if k not in d:
				return 0
			i = i // 10
		return 1

	def nthprimedigitsnumber(self, n):
		l = []
		for i in range(10000):
			if Solution.digit(self, i):
				l.append(i)
		return l[n]


class Solution:

	def nthprimedigitsnumber(self, n):
		primedigit = [2, 3, 5, 7]
		ourSeries = []
		for i in range(1, 2278):
			flag = True
			temp = i
			while i:
				if i % 10 not in primedigit:
					flag = False
					break
				i = i // 10
			if flag == True:
				ourSeries.append(temp)
		return ourSeries[n - 1]


class Solution:

	def nthprimedigitsnumber(self, n):
		ourSeq = [2, 3, 5, 7, 22, 23, 25, 27, 32, 33, 35, 37, 52, 53, 55, 57, 72, 73, 75, 77, 222, 223, 225, 227, 232, 233, 235, 237, 252, 253, 255, 257, 272, 273, 275, 277, 322, 323, 325, 327, 332, 333, 335, 337, 352, 353, 355, 357, 372, 373, 375, 377, 522, 523, 525, 527, 532, 533, 535, 537, 552, 553, 555, 557, 572, 573, 575, 577, 722, 723, 725, 727, 732, 733, 735, 737, 752, 753, 755, 757, 772, 773, 775, 777, 2222, 2223, 2225, 2227, 2232, 2233, 2235, 2237, 2252, 2253, 2255, 2257, 2272, 2273, 2275, 2277]
		return ourSeq[n - 1]


import math

class Solution:

	def nthprimedigitsnumber(self, n):
		x = math.log(3 * n + 4, 4)
		if x == int(x):
			num_digits = int(x) - 1
		else:
			num_digits = int(x)
		new_num = int(n - (4 ** num_digits - 4) / 3 - 1)
		final_answer = ''
		for i in range(num_digits):
			val = new_num % 4
			if val == 0:
				final_answer = '2' + final_answer
			if val == 1:
				final_answer = '3' + final_answer
			if val == 2:
				final_answer = '5' + final_answer
			if val == 3:
				final_answer = '7' + final_answer
			new_num = new_num // 4
		return final_answer


class Solution:

	def nthprimedigitsnumber(self, n):
		from collections import deque
		x = deque()
		for y in ['2', '3', '5', '7']:
			x.append(y)
		while n and len(x) < n:
			item = x.popleft()
			x.append(item + '2')
			x.append(item + '3')
			x.append(item + '5')
			x.append(item + '7')
			n -= 1
		while n:
			item = x.popleft()
			n -= 1
		return int(item)


class Solution:

	def nthprimedigitsnumber(self, n):
		nums = ['2', '3', '5', '7']
		if n <= 4:
			return nums[n - 1]
		start = 0
		stop = 4
		t = 0
		for i in range(4, n):
			if t == stop:
				start += 1
				t = 0
			nums.append(nums[start] + nums[t])
			t += 1
		return nums[n - 1]


from collections import deque

class Solution:

	def nthprimedigitsnumber(self, n):
		q = deque()
		ans = []
		q.append(2)
		q.append(3)
		q.append(5)
		q.append(7)
		c = 0
		while n > 0:
			cur = q.popleft()
			c += 1
			if c == n:
				return cur
			ans.append(cur)
			q.append(cur * 10 + 2)
			q.append(cur * 10 + 3)
			q.append(cur * 10 + 5)
			q.append(cur * 10 + 7)
		return -1


class Solution:

	def eachprime(self, x):
		c = 0
		for i in range(1, x + 1):
			if x % i == 0:
				c += 1
		if c == 2:
			return True
		return False

	def nthprimedigitsnumber(self, n):
		count = 0
		k = 2
		while count != n:
			flag = True
			for i in str(k):
				if not self.eachprime(int(i)):
					flag = False
					break
			if flag:
				count += 1
				if count == n:
					return k
			k += 1


class Solution:

	def nthprimedigitsnumber(self, n):
		s = set('2357')
		count = 0
		i = 2
		while count < n:
			Flag = 1
			for j in str(i):
				if j not in s:
					Flag = 0
			if Flag == 1:
				count += 1
			i += 1
		return i - 1


class Solution:

	def checkforeachdigitprime(self, N):
		while N:
			m = N % 10
			if m == 2 or m == 3 or m == 5 or (m == 7):
				N = N // 10
			else:
				return False
		return True

	def nthprimedigitsnumber(self, n):
		if n == 0:
			return 7
		if n == 1:
			return 2
		st = 1
		en = 1
		while st <= n:
			en += 1
			if self.checkforeachdigitprime(en) == True:
				st += 1
		return en


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

def control(num):
	if num == 0:
		return False
	while num:
		if not isPrime(num % 10):
			return False
		num //= 10
	return True

def main(k):
	counter = 0
	i = 0
	while counter < k:
		if control(i):
			counter += 1
		if counter == k:
			return i
		i += 1

class Solution:

	def nthprimedigitsnumber(self, n):
		return main(n)


class Solution:

	def nthprimedigitsnumber(self, n):
		s = {'2', '3', '5', '7'}
		i = 1
		while n:
			p = str(i)
			flag = False
			for c in p:
				if c not in s:
					flag = True
					break
			if not flag:
				n -= 1
			i += 1
		return i - 1


def prime_digits(n):
	count = 0
	i = 2
	while count <= n:
		flag = True
		for c in str(i):
			if c not in '2357':
				flag = False
		if flag:
			count += 1
			if count == n:
				return i
		i += 1

class Solution:

	def nthprimedigitsnumber(self, n):
		return prime_digits(n)


class Solution:

	def nthprimedigitsnumber(self, n: int) -> int:
		composit_digits = '146890'
		result = None
		i = 0
		number = 0
		while i < n:
			for digit in composit_digits:
				if digit in str(number):
					break
			else:
				i += 1
			if i == n:
				return number
			number += 1
		return number


class Solution:

	def nthprimedigitsnumber(self, n):

		def prime1(n1, l1):
			while n1 > 0:
				rem = n1 % 10
				n1 = n1 // 10
				if rem not in l1:
					return False
			return True
		l1 = [2, 3, 5, 7]
		i = 2
		count = 0
		while count < n:
			if prime1(i, l1):
				count += 1
			if count == n:
				return i
			i += 1


import math

class Solution:

	def nthprimedigitsnumber(self, n):
		a = 4
		ans = 0
		for i in range(1, 18):
			if a >= 3 * n / 4 + 1:
				l = i
				break
			ans += a
			a *= 4
		fa = ''
		for i in range(1, l + 1):
			for j in ['2', '3', '5', '7']:
				if ans + 4 ** (l - i) >= n:
					fa += j
					break
				else:
					ans += 4 ** (l - i)
		return int(fa)


class Solution:

	def checkeachdigitprime(self, j):
		while j != 0:
			digit = j % 10
			if digit == 2 or digit == 3 or digit == 5 or (digit == 7):
				j = j // 10
			else:
				return 0
		return 1

	def nthprimedigitsnumber(self, n):
		if n == 0:
			return 7
		if n == 1:
			return 2
		cnt = 0
		num = 1
		while cnt < n:
			num += 1
			if self.checkeachdigitprime(num) == 1:
				cnt += 1
		return num


class Solution:

	def nthprimedigitsnumber(self, n):
		count = n
		start = 2
		prime = [2, 3, 5, 7]
		while count != 0:
			counter = 0
			a = str(start)
			for i in range(len(a)):
				if int(a[i]) in prime:
					counter += 1
			if counter == len(a):
				count -= 1
			start += 1
		return start - 1


class Solution:

	def nthprimedigitsnumber(self, n):
		queue = ['2', '3', '5', '7']
		result = []
		for i in range(n):
			item = queue.pop(0)
			result.append(item)
			queue.append(item + '2')
			queue.append(item + '3')
			queue.append(item + '5')
			queue.append(item + '7')
		return int(result[n - 1])


class Solution:

	def nthprimedigitsnumber(self, n):
		q = []
		q.append(2)
		q.append(3)
		q.append(5)
		q.append(7)
		counter = 0
		while True:
			curNum = q.pop(0)
			counter += 1
			if counter == n:
				return curNum
			q.append(curNum * 10 + 2)
			q.append(curNum * 10 + 3)
			q.append(curNum * 10 + 5)
			q.append(curNum * 10 + 7)


class Solution:

	def is_prime(self, n):
		if n <= 1:
			return False
		for i in range(2, int(n ** 0.5) + 1):
			if n % i == 0:
				return False
		return True

	def is_primeDigit(self, n):
		for i in str(n):
			if not self.is_prime(int(i)):
				return False
		return True

	def nthprimedigitsnumber(self, n):
		(c, i) = (0, 2)
		while True:
			if self.is_primeDigit(i):
				c += 1
				if c == n:
					break
			i += 1
		return i


class Solution:

	def prime(self, n):
		a = ['2', '3', '5', '7']
		d = []
		j = 0
		c = 0
		for i in range(10001):
			for k in str(i):
				if k in a:
					c += 1
			if c == len(str(i)):
				d.append(i)
			c = 0
		return d[n - 1]

	def nthprimedigitsnumber(self, n):
		a = self.prime(n)
		return a


class Solution:

	def __init__(self):
		self.ans = [0] * 101

	def nthprimedigitsnumber(self, n):
		self.generate()
		return self.ans[n]

	def generate(self):
		i = 2
		count = 0
		while count != 100:
			n = i
			while n != 0:
				r = n % 10
				if r in [2, 3, 5, 7]:
					n = n // 10
				else:
					break
			if n == 0:
				count += 1
				self.ans[count] = i
			i += 1


class Solution:

	def nthprimedigitsnumber(self, n):
		a = [2, 3, 5, 7]
		a1 = ['2', '3', '5', '7']
		if n <= 4:
			return a[n - 1]
		for i in range(8, 10000):
			t = str(i) + 'd'
			for j in t:
				if j == 'd':
					a.append(i)
				elif j not in a1:
					break
				else:
					continue
			if len(a) == n:
				return i
			else:
				continue


class Solution:

	def nthprimedigitsnumber(self, n):
		list1 = [2, 3, 5, 7]
		a = 2
		c = 0
		start = 0
		while start != n:
			m = a
			while m > 0:
				s = m % 10
				if s in list1:
					m = m // 10
				else:
					break
			if m == 0:
				start += 1
			a += 1
		return a - 1


class Solution:

	def isprime(self, no):
		while no > 0:
			x = no % 10
			if x != 2 and x != 3 and (x != 5) and (x != 7):
				return 0
			no = no // 10
		return 1

	def nthprimedigitsnumber(self, n):
		c = 0
		x = 2
		while c < n:
			if Solution.isprime(self, x):
				c += 1
			x += 1
		return x - 1


class Solution:

	def nthprimedigitsnumber(self, n):
		pr = [2, 3, 5, 7]
		res = ''
		while n > 0:
			r1 = n % 4
			if r1 == 0:
				res += '7'
				n = n // 4
				n -= 1
				continue
			res += str(pr[r1 - 1])
			n = n // 4
		return int(res[::-1])


class Solution:

	def nthprimedigitsnumber(self, n):
		t = ''
		s = {1: '2', 2: '3', 3: '5', 0: '7'}
		while n != 0:
			t += s[n % 4]
			if n % 4 == 0:
				n = n - 1
			n = n // 4
		return t[::-1]


class Solution:

	def nthprimedigitsnumber(self, n):
		primeArr = [0] * 100
		primeArr[0] = 2
		primeArr[1] = 3
		primeArr[2] = 5
		primeArr[3] = 7
		j = 0
		for i in range(4, 100, 4):
			primeArr[i] = primeArr[j] * 10 + primeArr[0]
			primeArr[i + 1] = primeArr[j] * 10 + primeArr[1]
			primeArr[i + 2] = primeArr[j] * 10 + primeArr[2]
			primeArr[i + 3] = primeArr[j] * 10 + primeArr[3]
			j += 1
		return primeArr[n - 1]


class Solution:

	def nthprimedigitsnumber(self, n):
		count = 0
		num = 1
		while count < n:
			s = str(num)
			flag = True
			p = ['2', '3', '5', '7']
			for i in s:
				if i not in p:
					flag = False
			if flag:
				count += 1
			if count == n:
				return num
			num += 1


class Solution:

	def nthprimedigitsnumber(self, n):
		r = [2, 3, 5, 7]
		k = []
		i = 2
		while len(k) != n:
			t = str(i)
			s = 0
			for j in t:
				j = int(j)
				if j not in r:
					s = s + 1
					break
			if s == 0:
				k.append(i)
			i = i + 1
		return k[-1]


class Solution:

	def nthprimedigitsnumber(self, n):
		num = ''
		while n > 0:
			unit = n % 4
			if unit == 1:
				num += '2'
			elif unit == 2:
				num += '3'
			elif unit == 3:
				num += '5'
			elif unit == 0:
				num += '7'
			if unit == 0:
				n = n - 1
			n //= 4
		ans = num[::-1]
		return ans


class Solution:

	def nthprimedigitsnumber(self, n):
		res = []
		divisor = 4
		while n > 0:
			rem = n % divisor
			digit = str(self.apply4Digit(rem, divisor))
			res.append(digit)
			n -= divisor
			divisor *= 4
		s = ''.join(res[::-1])
		return s

	def apply4Digit(self, rem, div):
		div //= 4
		(low, high) = (1, div)
		if low <= rem <= high:
			return 2
		(low, high) = (high + 1, high + div)
		if low <= rem <= high:
			return 3
		(low, high) = (high + 1, high + div)
		if low <= rem <= high:
			return 5
		return 7


class Solution:

	def nthprimedigitsnumber(self, n):

		def helper(m):
			x = ['2', '3', '5', '7']
			for i in str(m):
				if i not in x:
					return False
			return True
		count = 0
		for i in range(2, 100000):
			if helper(i):
				count += 1
				if count == n:
					return i


import re

class Solution:

	def nthprimedigitsnumber(self, n):
		self.a = 0
		self.c = 0
		while n > 0:
			self.a = self.a + 1
			self.b = re.findall('[014689]', str(self.a))
			if self.b == []:
				n = n - 1
				self.c = self.c + 1
		return self.a


class Solution:

	def nthprimedigitsnumber(self, n):

		def prime1(n1, l1):
			while n1 > 0:
				rem = n1 % 10
				n1 = n1 // 10
				if rem not in l1:
					return False
			return True
		l1 = [2, 3, 5, 7]
		i = 2
		count = 0
		while count < n:
			if prime1(i, l1):
				count += 1
			if count == n:
				return i
			i += 1
import math
if __name__ == '__main__':
	t = int(input())
	for tcs in range(t):
		n = int(input())
		ob = Solution()
		print(ob.nthprimedigitsnumber(n))


class Solution:

	def nthprimedigitsnumber(self, n):

		def prime(n1, arr):
			while n1 > 0:
				r = n1 % 10
				n1 = n1 // 10
				if r not in arr:
					return False
			return True
		arr = [2, 3, 5, 7]
		i = 2
		count = 0
		while count < n:
			if prime(i, arr):
				count += 1
			if count == n:
				return i
			i += 1


class Solution:

	def nthprimedigitsnumber(self, n):
		count = 0
		arr = ['2', '3', '5', '7']
		i = 1
		while count != n:
			flag = 0
			for digit in str(i):
				if digit not in arr:
					flag = 1
					break
				else:
					pass
			if flag == 0:
				count += 1
			i += 1
		return i - 1


class Solution:

	def nthprimedigitsnumber(self, n):
		s = ['0', '1', '4', '6', '8', '9']
		A = []
		for j in range(1, 10000):
			flag = 0
			k = str(j)
			for i in k:
				if i in s:
					flag = 1
					break
			if flag == 0:
				A.append(j)
		return A[n - 1]


class Solution:

	def nthprimedigitsnumber(self, n):

		def isPrime(x):
			if x == 2 or x == 3 or x == 5 or (x == 7):
				return True
			return False
		if n == 1:
			return 2
		if n == 2:
			return 3
		if n == 3:
			return 5
		if n == 4:
			return 7
		i = 21
		c = 0
		while c < n - 4:
			i += 1
			temp = i
			while temp != 0:
				rem = temp % 10
				if not isPrime(rem):
					break
				temp = temp // 10
			if temp == 0:
				c += 1
		return i