PAT 1015. Reversible Primes (20)

#include<stdio.h>
int main(){
	int n, d;
	int reversiblePrime = 0;
	while(scanf("%d%d\n", &n,&d) == 2 && n >=0){
		if(prime(n) && prime(reversPrime(n,d)))
			printf("Yes\n");
		else
			printf("No\n");
	}
	return 0;
}

int prime(int n){	
	if(n == 2) return 1;
	if(n%2 == 0 || n ==1) return 0;
	int i;
	for( i =3; i <=n/2; i++){
		if(n%i == 0) return 0;
	}
	return 1;
}

int reversPrime(int n, int d){
	int reversiblePrime = 0;
	while(n != 0){
		reversiblePrime = reversiblePrime*d + n%d;
		n /= d;
	}
	return reversiblePrime;
}

 

你可能感兴趣的:(Prim)