UVA 10235 - Simply Emirp Solution
UVA 10235 - Simply Emirp Solution
/*
It is a simple and easy problem. But has critical test case.
Critical case:
If a number is prime and reverse number is equal to the given number then the output is "prime" not "emirp".
For example:
Given number 11.
11 number is prime. But reverse number is also 11.So, it is not "emirp". It is prime.
*/
Code:
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#define MAX 1000001
using namespace std;
bool prime[MAX];
int reverseInt(int num)
{
string str=to_string(num);
reverse(str.begin(),str.end());
num=stoi(str);
return num;
}
int main()
{
int i,j,x;
memset(prime,1,sizeof(prime));
for(i=4;i<MAX;i+=2)
prime[i]=0;
for(i=3;i<1001;i+=2)
{
if(prime[i])
{
for(j=i*i;j<MAX;j+=i)
prime[j]=0;
}
}
while(cin>>x)
{
if(prime[x])
{
i=reverseInt(x);
if(prime[i] && x!=i)
cout<<x<<" is emirp."<<endl;
else
cout<<x<<" is prime."<<endl;
}
else
cout<<x<<" is not prime."<<endl;
}
return 0;
}
Input:
7
11
33
59
24
3
17
Output:
7 is prime.
11 is prime.
33 is not prime.
59 is prime.
24 is not prime.
3 is prime.
17 is emirp.
Comments
Post a Comment