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 is a reversible prime because its reverse is also a prime.
Now given any two positive integers and , you are supposed to tell if is a reversible prime with radix .
Input Specification:
The input file consists of several test cases. Each case occupies a line which contains two integers and . The input is finished by a negative .
Output Specification:
For each test case, print in one line Yes if is a reversible prime with radix , or No if not.
Sample Input:
73 10
23 2
23 10
-2
Sample Output:
Yes
Yes
No
using namespace std;
bool is_prime(int n){
if(n < 2) return false;
for(int i = 2; i <= n / i; i++)
if(n % i == 0) return false;
return true;
}
bool check(int x, int d){
if(!is_prime(x)) return false;
int res = 0;
while(x){
res = res * d + x % d;
x /= d;
}
return is_prime(res);
}
int main(){
int n, d;
while(cin >> n >> d, n > 0){
if(check(n, d)) puts("Yes");
else puts("No");
}
return 0;
}