A reversible prime in any number system was a prime whose "reverse" in the that number system is also a prime. For example in the decimal system are a reversible prime because its reverse PNS is also a prime.
Now given any of positive integers n (<5) and D (1 < D <=), you be supposed to tell if N is a Reversi Ble prime with radix D.
Input Specification:
The input file consists of several test cases. Each case occupies a line which contains the integers N and D. The input is finished by a negative N.
Output Specification:
For the test case, the print in one line "Yes" if N is a reversible prime with radix D, or "No" if not.
The problem is that the number of prime N is reversed in radix, and it is also prime in decimal.
23 is prime, binary means 10111, inversion 11101, decimal number is 29, I'm tangled.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h>bool isprime (int num) {if (num==2) return true; if (num<2) return false; int temp= (int) sqrt (num*1.0); for (int i=2;i<=temp;i++) if (num%i==0) return false; return true; }int Main () {int d,n; while (scanf ("%d", &n) &&n>=0) {int revers[100000],i=0,num=0; scanf ("%d", &d); if (IsPrime (n)) {while (n) {revers[i++]=n%d; n=n/d; printf ("%d", revers[i-1]); } for (int j=0;j<i-1;j++) {num= (revers[j]+num) *d; } Num+=revers[i-1]; printf ("\n%d", num); if (IsPrime (num)) printf ("yes\n"); else printf ("no\n"); } else printf ("no\n"); } system ("Pause"); return 0; }
1015. Reversible Primes