2. 1 和 0 不是質數
test input:
999
481184
373
998001
998857
753257
823455
999999
850058
78887
999983
test output:
999 is not prime.
481184 is not prime.
373 is prime.
998001 is not prime.
998857 is emirp.
753257 is prime.
823455 is not prime.
999999 is not prime.
850058 is not prime.
78887 is prime.
999983 is emirp.
(test input, output from :
http://acm.uva.es/board/viewtopic.php?f=38&t=76675&p=368862&hilit=10235&sid=abfeae4866641eb3eff8acaf0850d1e2#p368862)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int prime(int num){
int limit;
int i;
if(num==1 || num == 0) return 0;
limit=sqrt(num);
for(i=2;i<=limit;i++){
if(num%i==0)
return 0;
}
return 1;
}
int emirp(int num){
char rever[1000000]={0};
int i=0;
int num2;
if(num < 13) return 0;
num2=num;
while(num2!=0){
rever[i++]=(num2%10)+'0';
num2/=10;
}
rever[i]='\0';
num2=atoi(rever);
if(num==num2) return 0;
if(prime(num2)==1)
return 1;
else
return 0;
}
int main(void){
int n;
while(scanf("%d",&n)!=EOF){
if(n==1 || n==0){
printf("%d is not prime.\n",n);
continue;
}
if(prime(n)==1){
if(emirp(n)){
printf("%d is emirp.\n",n);
}else{
printf("%d is prime.\n",n);
}
}else{
printf("%d is not prime.\n",n);
}
}
return 0;
}
沒有留言:
張貼留言