#include <stdio.h>
#include <math.h>
int prime(int);//用來判斷是否為質數
int swap(int);//用來翻轉數字
int cycle(int);//用來驗證是否為迴文質數
int main(){
int m;
int i;
while(scanf("%d", &m)!=EOF){
if(prime(m) != 0 && m>=10 && prime(swap(m)) != 0 ){
if(cycle(m) == 0){
printf("%d is prime.\n", m);
}
else{
printf("%d is emirp.\n", m);
}
}
else if(prime(m) == 0){
printf("%d is not prime.\n", m);
}
else{
printf("%d is prime.\n", m);
}
}
return 0;
}
int prime(int N){
int a;
int counter;
counter = 1;
a = 2;
while(a <= sqrt(N)){
if(N%a != 0){
a++;
}
else{
counter = 0;
break;
}
}
return counter;
}
int swap(int M){
int a[100];
int i,j;
int total;
i=total=0;
while(M != 0){
a[i] = M%10;
M = M/10;
i++;
}
for(j=0; j<i; j++){
total = (total*10)+a[j];
}
return total;
}
int cycle(int K){
int s[35];
int i,j,k,counter;
i = counter = 0;
while(K != 0){
s[i] = K%10;
K = K/10;
i++;
}
for(j=0, k=i-1; j<=k; j++, k--){
if(s[j] != s[k]){
counter = 1;
}
}
return counter;
}
#include
#include
int prime(int);//用來判斷是否為質數
int swap(int);//用來翻轉數字
int cycle(int);//用來驗證是否為迴文質數
int main(){
int m;
int i;
while(scanf("%d", &m)!=EOF){
if(prime(m) != 0 && m>=10 && prime(swap(m)) != 0 ){
if(cycle(m) == 0){
printf("%d is prime.\n", m);
}
else{
printf("%d is emirp.\n", m);
}
}
else if(prime(m) == 0){
printf("%d is not prime.\n", m);
}
else{
printf("%d is prime.\n", m);
}
}
return 0;
}
int prime(int N){
int a;
int counter;
counter = 1;
a = 2;
while(a <= sqrt(N)){
if(N%a != 0){
a++;
}
else{
counter = 0;
break;
}
}
return counter;
}
int swap(int M){
int a[100];
int i,j;
int total;
i=total=0;
while(M != 0){
a[i] = M%10;
M = M/10;
i++;
}
for(j=0; j<i; j++){
total = (total*10)+a[j];
}
return total;
}
int cycle(int K){
int s[35];
int i,j,k,counter;
i = counter = 0;
while(K != 0){
s[i] = K%10;
K = K/10;
i++;
}
for(j=0, k=i-1; j<=k; j++, k--){
if(s[j] != s[k]){
counter = 1;
}
}
return counter;
}
第一個if條件中m>=10要改掉。
沒有查好迴文質數的定義就丟上來,抱歉..