Wednesday, June 14, 2017

uva problem solution 10235 - Simply Emirp

problem link:

Discuss: in this problem you just have to check weather a number is prime or not.you have do it with square root. and then reverse the number and do check again prime number criteria. if it fulfill then it is empri number and obvisouly it should be not equal to previous number. if it is then it is prime


try yourself before see the code



#include<bits/stdc++.h>
using namespace std;
int main()
{
   long long i,j,n,p,q,cnt,flag,s;


   while(cin >>p)
   {
       q=0;
   cnt=1;flag=1;
     for(i=2;i<=sqrt(p);i++)
     {
        if(p%i==0)
        {
          flag=0;
          break;
        }
     }
      if(flag==0)
     cout <<p<<" is not prime."<<endl;
     else
     {
     for(i =p;i!=0;i=i/10)
        {
                q = q*10+i%10;
}
     for(i=2;i<=sqrt(q);i++)
     {

        if(q%i==0)
        {
        cnt=0;
        break;
        }
     }


     if(cnt==1&&p!=q)
     cout <<p<<" is emirp."<<endl;
     else
     cout <<p<<" is prime."<<endl;
   }
   }
   return 0;
}

No comments:

Post a Comment