1015. Reversible Primes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.