Question
print twisted prime number in an (array for java/list for python).
(A number is called a twisted prime number if it is a prime number and reverse of this number is also a prime number.)
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
Java
Python
Java
import java.util.Scanner;
public class TwistedPrimeInGivenArray
{
/* reverseOfNumber() reverses a number */
public static int reverseOfNumber(int num)
{
int temp=0,reversedInteger=0,remainder=0;
temp=num;
while( temp > 0 )
{
remainder = temp % 10;
reversedInteger = reversedInteger * 10 + remainder;
temp /= 10;
}
return reversedInteger;
}
/* isPrime() checks whether a number is prime number or not */
public static boolean isPrime(int num)
{
boolean flag = true;
if(num>1)
{
for (int j = 2; j <= num / 2; j++)
{
if ((num % j) == 0)
{
flag = false;
break;
}
}
}
else
{
flag=false;
}
return flag;
}
public static void main(String[] args)
{
int reversedNumber=0,size=0;
int arr[];
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of Array:");
size = sc.nextInt();
arr=new int[size];
System.out.println("Enter elements in Array:");
for(int i=0;i
Python
def isPrimeNumber(n):
isPrime=True
if(n>1):
for i in range(2,n):
if(n%i==0):
isPrime=False
break
else:
isPrime = False
return isPrime
def reverseOfNumber(n):
reversedInteger= 0
temp= n
while(temp>0):
remainder=temp%10
reversedInteger=reversedInteger*10+remainder
temp=temp//10
return reversedInteger
if(__name__=='__main__'):
size=int(input("Enter size of list:"))
li=[]
print("Enter Elements in list:")
for i in range(0,size):
print((i+1),":",end="")
li.append(int(input()))
print("Twisted Prime Number(s) in given list:")
for number in li:
reversedNumber=reverseOfNumber(number)
if(isPrimeNumber(number)==True and isPrimeNumber(reversedNumber)==True):
print(number,end=" ")