1015. Reversible Primes (20)

考察素数以及非10进制到10进制数的转化

#include<iostream>
#include<stack>
#include<math.h>

int ReverseNumber(int N, int D)
{
	int sum = 0;
	do
	{
		sum = sum*D + N%D;
		N /= D;
	}while(N != 0);
	return sum;
}

bool IsPrime(int n)
{
	//special case
	if(n == 0 || n == 1) 
		return false;
	for(int i = 2; i <= sqrt((double)n); ++i)
	{
		if(n%i == 0)
			return false;
	}
	return true;
}
int main()
{
	int N, D;
	while(scanf("%d",&N) != EOF && N >= 0)
	{
		scanf("%d",&D);
		if(IsPrime(N) && IsPrime(ReverseNumber(N,D)))
			printf("Yes\n");
		else printf("No\n");
	}
	return 0;
}


 

你可能感兴趣的:(pat,ZJU)