ACM 10235 Q10235: Simply Emirp
作法 : 質數判斷...
/******************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int math[5200]={0};
int p;
int prime()
{
char num[100000]={0};
int a,b,m=0;
for(a=2;a<50000;a++)
if(num[a]==0)
{
math[m]=a;
m++;
for(b=2;a*b<=50000;b++)
num[a*b]=1;
}
return m;
}
int primeJudge (int num)
{
int a,yes=0,s=(int)sqrt(num);
for(a=0;a<p&&math[a]<=s;a++)
if(num%math[a]==0)
{yes=1;break;}
if(yes==0)
return 1;
else
return 0;
}
int change (int num)
{
int n=0;
while(num)
{
n=n*10+num%10;
num/=10;
}
return n;
}
main()
{
p=prime();
int n;
while(scanf("%d",&n)==1)
{
if(n==1) break;
int yes1=primeJudge(n),emirp=change(n);
int yes2=primeJudge(emirp);
if(yes1==1&&yes2==1&&n!=emirp)
printf("%d is emirp.\n",n);
else if((yes1==1&&yes2==0)||(yes1==1&&yes2==1&&n==emirp))
printf("%d is prime.\n",n);
else
printf("%d is not prime.\n",n);
}
return 0;
}