Given a number N. Check whether N is a Twisted Prime number or not.
Note: A number is called Twisted Prime if it is a prime and its reverse is also a prime.
Example 1:
Input: N = 97
Output: 1
Explanation: 97 is a prime number. Its 
reverse 79 isalso a prime number. Thus 
97 is a  twisted Prime and so, answer is 1.
Example 2:
Input: N = 43
Output: 0
Explanation: 43 is a prime number but its 
reverse 34 is not a prime.So, 43 is not a 
twisted prime and thus, answer is 0.
Your Task:You don't need to read input or print anything.Your task is to complete the function isTwistedPrime() which takes a number N as input parameter and returns 1 if it is a twisted prime number.Otherwise, it returns 0.
Expected Time Complexity:(O(sqrt(max(N,RevN))), here RevN is the reverse of N.
Expected Space Complexity:O(1)
Constraints:
1<=N<=10^{9}

Source: geeksforgeeks | Difficulty: EASY
Tags: Number theory

Starter code: #User function Template for python3



class Solution:

    def isTwistedPrime(self,N):

        #code here

Solutions:
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 isTwistedPrime(self, N):
		s = str(N)
		s = s[::-1]
		if fun(N) and fun(int(s)):
			return 1
		return 0


class Solution:

	def isTwistedPrime(self, N):
		flag = 1
		for i in range(2, int(N ** 0.5) + 1):
			if N % i == 0:
				flag = 0
				break
			flag = 1
		x = str(N)
		x = x[::-1]
		x = int(x)
		flag1 = 1
		for i in range(2, int(x ** 0.5) + 1):
			if x % i == 0:
				flag1 = 0
				break
			flag1 = 1
		if flag == 1 and flag1 == 1:
			return 1
		return 0


from math import sqrt

class Solution:

	def isTwistedPrime(self, N):
		flag1 = self.prime(N)
		rev = self.reverse(N)
		flag2 = self.prime(rev)
		if flag1 and flag2:
			return 1
		return 0

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

	def reverse(self, N):
		rev = 0
		while N:
			rev = rev * 10
			digit = N % 10
			rev += digit
			N //= 10
		return rev


import math as m

class Solution:

	def isTwistedPrime(self, N):

		def prime(n):
			if n == 0 or n == 1:
				return False
			k = round(m.sqrt(n)) + 1
			for i in range(2, k):
				if n % i == 0:
					return False
			return True
		k = str(N)
		if prime(N) == True and prime(int(k[::-1])) == True:
			return 1
		return 0


import math

def is_prime(n):
	if n == 1:
		return False
	elif n == 2:
		return True
	elif n % 2 == 0:
		return False
	else:
		for x in range(3, int(math.sqrt(n)) + 1, 2):
			if n % x == 0:
				return False
				break
		return True

class Solution:

	def isTwistedPrime(self, N):
		x = str(N)
		y = int(x[::-1])
		if is_prime(N) == True & is_prime(y) == True:
			return 1
		return 0


class Solution:

	def isTwistedPrime(self, N):

		def prime(n):
			if n == 0 or n == 1:
				return 0
			for i in range(2, int(n ** 0.5) + 1):
				if n % i == 0:
					return 0
			return 1
		x = int(str(N)[::-1])
		if prime(N) == 1 and prime(x) == 1:
			return 1
		return 0


from math import sqrt

class Solution:

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

	def isTwistedPrime(self, N):
		return self.isPrime(N) and self.isPrime(int(str(N)[::-1]))


import math

class Solution:

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

	def isTwistedPrime(self, N):
		flag = self.isPrime(N)
		strr = str(N)
		strr = strr[::-1]
		N = int(strr)
		flag1 = self.isPrime(N)
		return 1 if flag == 1 and flag1 == 1 else 0


import math

class Solution:

	def isTwistedPrime(self, N):
		a = str(N)
		a = a[::-1]
		a = int(a)
		for i in range(2, int(math.sqrt(N)) + 1):
			if N % i == 0:
				return 0
		for i in range(2, int(math.sqrt(a)) + 1):
			if a % i == 0:
				return 0
		return 1


class Solution:

	def isTwistedPrime(self, N):
		s = str(N)
		s = s[::-1]
		s = int(s)
		for i in range(2, int(N ** 0.5) + 1):
			if N % i == 0:
				return 0
		for i in range(2, int(s ** 0.5) + 1):
			if s % i == 0:
				return 0
		return 1


from math import sqrt

class Solution:

	def isTwistedPrime(self, N):
		revno = int(str(N)[::-1])
		if self.prime(N) and self.prime(revno):
			return 1
		return 0

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


class Solution:

	def isTwistedPrime(self, N: int) -> int:

		def reverse(n: int) -> int:
			return int(str(N)[::-1])

		def is_prime(n: int) -> bool:
			if n < 2:
				return False
			else:
				for i in range(2, int(math.sqrt(n)) + 1):
					if n % i == 0:
						return False
				return True
		condition1 = is_prime(N)
		condition2 = is_prime(reverse(N))
		if condition1 and condition2:
			return 1
		else:
			return 0


class Solution:

	def isTwistedPrime(self, N):

		def h(n):
			if n == 0 or n == 1:
				return False
			for i in range(2, int(n ** 0.5) + 1):
				if n % i == 0:
					return False
			return True
		if h(N) and h(int(str(N)[::-1])):
			return 1
		return 0


class Solution:

	def isTwistedPrime(self, N):

		def prime(n):
			if n == 1:
				return False
			for i in range(2, int(n ** 0.5) + 1):
				if n % i == 0:
					return False
			return True
		if prime(N) and prime(int(str(N)[::-1])):
			return 1
		else:
			return 0


class Solution:

	def isTwistedPrime(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
		if isPrime(N) == 0:
			return 0
		r = ''
		while N > 0:
			r += str(N % 10)
			N = N // 10
		if isPrime(int(r)) == 0:
			return 0
		return 1


import math

class Solution:

	def isTwistedPrime(self, N):
		reversedString = str(N)[::-1]
		reversedNumber = int(reversedString)
		s = max(N, reversedNumber)
		s = int(math.sqrt(s))
		for i in range(2, s + 1):
			if N % i == 0:
				return 0
			if reversedNumber % i == 0:
				return 0
		return 1


class Solution:

	def isTwistedPrime(self, N):
		if N == 1 or N == 10:
			return 0
		import math
		a = str(N)
		a = a[::-1]
		a = int(a)
		s = max(N, a)
		s = int(math.sqrt(s))
		for i in range(2, s + 1):
			if N % i == 0:
				return 0
			if a % i == 0:
				return 0
		return 1


class Solution:

	def isPrime(self, n):
		if n == 0 or n == 1:
			return 0
		if n % 2 == 0 or n % 3 == 0:
			return 0
		for i in range(5, int(n ** 0.5), 6):
			if n % i == 0 or n % (i + 2) == 0:
				return 0
		return 1

	def isTwistedPrime(self, N):
		a = str(N)
		reverse = int(a[::-1])
		if self.isPrime(N) and self.isPrime(reverse):
			return 1
		return 0
import math
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isTwistedPrime(N))


class Solution:

	def isTwistedPrime(self, N):
		n1 = N
		n2 = int(str(n1)[::-1])
		for i in range(2, int(math.sqrt(n1)) + 1):
			if n1 % i == 0:
				return 0
		for i in range(2, int(math.sqrt(n2)) + 1):
			if n2 % i == 0:
				return 0
		return 1
import math
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isTwistedPrime(N))


class Solution:

	def isprime(self, n):
		if n == 1:
			return 0
		flag = 1
		for i in range(2, int(n ** 0.5) + 1):
			if n % i == 0:
				flag = 0
				break
		return flag

	def isTwistedPrime(self, n):
		s = str(n)
		s = s[::-1]
		m = int(s)
		if self.isprime(n) and self.isprime(m):
			return 1
		return 0
import math
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isTwistedPrime(N))


import math

class Solution:

	def isPrime(self, n):
		if n == 2:
			return True
		for itr in range(2, math.ceil(math.sqrt(n)) + 1):
			if n % itr == 0:
				return False
		return True

	def isTwistedPrime(self, N):
		return 1 if self.isPrime(N) and self.isPrime(int(str(N)[::-1])) else 0
import math
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isTwistedPrime(N))


class Solution:

	def prime(self, R):
		k = int(R ** 0.5)
		for i in range(2, k + 1):
			if R % i == 0:
				return False
		return True

	def isTwistedPrime(self, N):
		if self.prime(N):
			d = str(N)
			d = d[::-1]
			d = int(d)
			if self.prime(d):
				return 1
		return 0
import math
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isTwistedPrime(N))


class Solution:

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

	def isTwistedPrime(self, N):
		k = str(N)
		if self.prime(int(k)) and self.prime(int(k[::-1])):
			return 1
		else:
			return 0
import math
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.isTwistedPrime(N))


class Solution:

	def isTwistedPrime(self, N):

		def is_prime(n):
			if n <= 1:
				return False
			elif n > 1:
				for i in range(2, int(n ** 0.5) + 1):
					if n % i == 0:
						return False
				return True
		a = N
		b = int(str(N)[::-1])
		if is_prime(a):
			if is_prime(b):
				return 1
		return 0


import math

class Solution:

	def isTwistedPrime(self, N):

		def prime(N):
			a = 0
			if N < 2:
				return 0
			elif N == 2 or N == 3 or N == 5 or (N == 7) or (N == 11):
				return 1
			elif N % 2 == 0 or N % 3 == 0 or N % 5 == 0 or (N % 7 == 0) or (N % 11 == 0):
				return 0
			else:
				i = 5
				while i * i <= N:
					if N % i == 0 or N % (i + 2) == 0:
						return 0
					i = i + 6
				return 1
		n = N
		f = prime(N)
		sum = 0
		while n > 0:
			e = n % 10
			sum = sum * 10 + e
			n = n // 10
		c = prime(sum)
		if c == 1 and f == 1:
			return 1
		else:
			return 0


class Solution:

	def isTwistedPrime(self, N):
		p = 0
		import math
		for i in range(2, int(math.sqrt(N)) + 1):
			if N % i == 0:
				p = 1
				break
		if p == 0:
			N = str(N)
			N = N[::-1]
			n = int(N)
			p = 0
			import math
			for i in range(2, int(math.sqrt(n)) + 1):
				if n % i == 0:
					p = 1
					break
			if p == 0:
				return 1
		return 0


class Solution:

	def isTwistedPrime(self, N):
		if N == 1:
			return 0
		else:
			for i in range(2, int(N ** 0.5) + 1):
				if N % i == 0:
					return 0
					break
			else:
				s = str(N)
				p = int(s[-1:-len(s) - 1:-1])
				for i in range(2, int(p ** 0.5) + 1):
					if p % i == 0:
						return 0
						break
				else:
					return 1


import math

class Solution:

	def isTwistedPrime(self, N):
		n = N
		nr = int(str(n)[::-1])
		for i in range(2, math.ceil(math.sqrt(n)) + 1):
			if n % i == 0:
				return 0
		for i in range(2, math.ceil(math.sqrt(nr)) + 1):
			if nr % i == 0:
				return 0
		return 1


from math import sqrt

def isPrime(num):
	flag = True
	for i in range(2, int(sqrt(num)) + 1):
		if num % i == 0:
			flag = False
			break
	return flag

def reverseNumber(n):
	ans = 0
	while n != 0:
		digit = n % 10
		ans = ans * 10 + digit
		n = n // 10
	return ans

class Solution:

	def isTwistedPrime(self, N):
		reversenum = reverseNumber(N)
		if isPrime(N) and isPrime(reversenum):
			return 1
		else:
			return 0


class Solution:

	def isTwistedPrime(self, N):

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


class Solution:

	def isTwistedPrime(self, N):
		rev_n = reverse(N)
		if isPrime(N) and isPrime(rev_n):
			return 1
		else:
			return 0

def isPrime(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 reverse(n):
	rev_num = 0
	while n > 0:
		rem = n % 10
		rev_num = rev_num * 10 + rem
		n = n // 10
	return rev_num


import math

class Solution:

	def isTwistedPrime(self, N):

		def isprime(x):
			c = 0
			for i in range(2, int(math.sqrt(x)) + 1):
				if x % i == 0:
					c = 1
					break
			if c == 0:
				return 1
			else:
				return 0
		x1 = isprime(N)
		x2 = str(N)
		x2 = x2[::-1]
		x3 = isprime(int(x2))
		if x1 == 1 and x3 == 1:
			return 1
		else:
			return 0


class Solution:

	def isTwistedPrime(self, N):
		if isprime(N) == 1 and isprime(rev(N)) == 1:
			return 1
		else:
			return 0

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

def rev(n):
	n1 = str(n)
	n1 = n1[::-1]
	n2 = int(n1)
	return n2


from math import sqrt

class Solution:

	def isTwistedPrime(self, N):
		N_rev = int(str(N)[::-1])
		flag = 0
		for i in range(2, int(sqrt(N)) + 1):
			if N % i == 0:
				flag = 1
				break
		if flag == 1:
			return 0
		else:
			for i in range(2, int(sqrt(N_rev)) + 1):
				if N_rev % i == 0:
					flag = 1
					break
		if flag == 0:
			return 1
		else:
			return 0


from math import sqrt

def isprime(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 isTwistedPrime(self, N):
		t = int(str(N)[::-1])
		if isprime(t) and isprime(N):
			return 1
		return 0