1015 Reversible Primes(20)
A reversible prime in any number system is a prime whose “reverse” in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime.
Now given any two positive integers N (<10^5) and D (1<D≤10), you are supposed to tell if N is a reversible prime with radix D.
Input Specification:
The input file consists of several test cases. Each case occupies a line which contains two integers N and D. The input is finished by a negative N.
Output Specification:
For each test case, print in one line Yes if N is a reversible prime with radix D, or No if not.
Sample Input:
73 10
23 2
23 10
-2
Sample Output:
Yes
Yes
No
Solution:
难点不在于判断素数, 而是理解不了进制转换。
先按d进制逆转, 将逆转后的数转成10进制, 判断素数.
d进制逆转属于中间过程, 用一个数组存放.
|
再附上我原先的求素数版本, 以便警示自己.// 原先的求素数
int is_prime(int a) {
if (a < 2) return 0;
if (a == 2) return 1;
int sq = sqrt(a);
for (int i = 2; i <= sq; i++) {
if (a % i == 0) return 0;
}
return 1;
}