[PYTHON](PAT)1015 REVERSIBLE PRIMES (20)

版权声明:首发于 www.amoshuang.com https://blog.csdn.net/qq_35499060/article/details/82077540

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 (<10​5​​) 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

题目大意

给定一些整型和进制。对于每一对来说,该整形是否是素数,如果否则输出No,如果是,那么该整形转换成指定的进制再反转再转换成10进制后是否是素数,如果是,则输出Yes,否则输出No。

注意

1>内置函数int可以把字符串从指定的进制转换成十进制

2>在检验是否是整型的时候,要注意0和1,这两个直接返回否

Python实现

import math
def check(num):
    if num <= 1:
        return 0
    flag = 1
    for x in range(2, round(math.sqrt(num))+1):
        if num % x ==0:
            flag = 0
            return flag
    return flag

def change(num, radix):
    result = ''
    while(num !=0):
        temp = num % radix
        result = str(temp) + result
        num = num //radix
    return result
                        
if __name__ == "__main__":
    while(True):
        line = input().split(" ")
        if len(line) < 2:
            break
        num = int(line[0])
        radix = int(line[1])
        if check(num) == 1:
            rever = change(num, radix)
            rever = rever[::-1]
            rever = int(rever, radix)
            if check(rever) == 1:
                print("Yes")
            else:
                print("No")
        else:
            print("No")

猜你喜欢

转载自blog.csdn.net/qq_35499060/article/details/82077540
今日推荐