A Twisted Prime is a number whose itself is a prime number and its reverse is also prime number .
To solve this problem following are the approaches should be taken:
1)Check the number is prime or not
2) If YES the REVERSE it and check whether the number is Prime or not.
Programme:
//TO CHECK NUMBER IS PRIME OR NOT AT TIMECOMPLEXITYO(SQRT(n)) isPrime()
#include<bits/stdc++.h>
using namespace std;
bool isPrime(int n)
{
if(n<=1)
return false;
if(n<=2)
return true;
if(n%2==0||n%3==0)
{
return false;
}
for(int i=5;i*i<=n;i=i+6)
{
if(n%i==0||n%(i+2)==0)
return false;
}
return true;
}
int revresee(int n)
{
int sum=0;
while(n>0)
{
int c=n%10;
sum=sum*10+c;
n=n/10;
}
return sum;
}
main()
{
int t;
cin>>t;
while(t>0)
{
int n,c;
cin>>n;
if(isPrime(n))
{
c=revresee(n);
//cout<<c;
if(isPrime(c))
{
cout<<"Yes"<<"\n";
}
else
cout<<"No"<<"\n";
}
else
cout<<"No"<<"\n";
}
}
For PRACTICE this question check this link:
https://practice.geeksforgeeks.org/problems/twisted-prime-number/0
No comments:
Post a Comment