C++
#include <iostream>
using namespace std;
int n;
bool is_prime(int n) //judge whether num n is prime
{
if (n < 2) return false;
for (int i = 2; i <= n / i; i++ ) {
if (n % i == 0) return false;
}
return true;
}
string convert_str(int n, int rdx) //10-radix -> R-radix
{
string res;
while (n > 0) {
int q = n / rdx;
int r = n % rdx;
res += (r + '0');
n = q;
}
//the "divide R and take the remainder" algorithm to convert a 10-radix num to R-radix automatically reverse the digit present in str form,
//so we don't have to reverse the str again, just return is OK
return res;
}
int convert_d10(string base, int rdx)
{
int res = 0;
//in Jiushao Qin algorithm, the convert to 10-radix process is computed in natural order, don't reverse
for (int i = 0; i < base.size(); i++ ) {
res = res * rdx + (base[i] - '0');
}
return res;
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int cnt; //control the output of "\n"
while (1) {
cin >> n;
if (n < 0) break; //the input n ends when n is negative
int rdx; cin >> rdx;
if (cnt) cout << endl; //from the second, cout << endl at first, this helps avoid the last endl
cnt++ ;
if (!is_prime(n)) {
cout << "No";
}
else {
string base = convert_str(n, rdx);
int dit10 = convert_d10(base, rdx);
if (is_prime(dit10)) {
cout << "Yes";
}
else {
cout << "No";
}
}
}
return 0;
}