test

练习1:实现计算求最大公约数和最小公倍数的函数。

# 最大公约数Greatest Common Division
def gcd(x, y):
    (x, y) = (y, x) if x > y else (x, y)
    for factor in range(x, 0, -1):
        if x % factor == 0 and y % factor == 0:
            return factor


# 最小公倍数Least Common multiple
def lcm(x, y):
    return x * y // gcd(x, y)


print(gcd(2, 5))

练习2:实现判断一个数是不是回文数的函数。

def is_Palindrome(x): # 123
    tmp = x # 123
    total = 0
    while tmp > 0:
        total = total * 10 + tmp % 10 # 3 30+2 320+1
        tmp //= 10 # 12 1
    return total == x

print(is_Palindrome(11))

练习3:实现判断一个数是不是素数的函数。

def is_Prime(x):
    for i in range(2, x):
        if x % i == 0:
            return False
    return True if x != 1 else False

print(is_Prime(1))

练习4:写一个程序判断输入的正整数是不是回文素数。

def is_Prime_Palindrome(x):
    if x > 0 and type(x) == int:
        if is_Prime(x) and is_Palindrome(x):
            return True

print(is_Prime_Palindrome(11))
posted @ 2024-01-28 01:09  七星海棠^_~  阅读(32)  评论(0)    收藏  举报