关闭

1015. Reversible Primes (20)

54人阅读 评论(0) 收藏 举报
分类:

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 (< 105) 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

#include<stdio.h>
#include<math.h>
int Trans(int n,int d);
int IsPrime(int n);
int main(){
    int num,D,reverse;
    while(scanf("%d",&num)==1&&num>0){
        scanf("%d",&D);
        reverse=Trans(num,D);
        if(IsPrime(reverse)&&IsPrime(num))printf("Yes\n");
        else printf("No\n");
    }

}
int IsPrime(int n){
    int i=0;
    if(n==0||n==1)return 0;
    for(i=2;i<=sqrt(n);i++){
        if(n%i==0)return 0;
    }
    return 1;
}
int Trans(int n,int d){
    int m=0;
    while(n!=0){
        m=m*d+n%d;
        n/=d;
    }
    return m;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:54596次
    • 积分:3315
    • 等级:
    • 排名:第10864名
    • 原创:286篇
    • 转载:8篇
    • 译文:0篇
    • 评论:10条
    最新评论