USACO 1.6 pprime Python3

"""
ID: ***
LANG: PYTHON3
TASK: pprime
"""
fin = open('pprime.in', 'r')
fout = open('pprime.out', 'w')
lower_Limit, upper_Limit = map(int, fin.readline().split())
maxL = len(str(upper_Limit))
minL = len(str(lower_Limit))
nums = [0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
str_Num = ""
enough = False


def isPrime(num):
    for x in range(2, num):
        if num % x == 0:
            return False
    return True


def generate_Palind(nums, chang, pos):
    global enough
    global str_Num
    if enough:
        return
    lang = int((chang + 1) / 2)
    if(pos > chang):
        if int(str_Num) > upper_Limit:
            enough = True
            return
        num = int(str_Num)
        if (isPrime(num) and num >= lower_Limit):
            fout.write("{}\n".format(num))
        str_Num = str_Num[:lang]
        return
    if pos <= lang:
        for i in range(1, 11):
            if pos == 1 and nums[i] == 0:
                continue
            if chang == len(str(lower_Limit)) and int(str_Num + str(nums[i])) < int(str(lower_Limit)[:pos]):
                continue
            str_Num += "{}".format(nums[i])
            generate_Palind(nums, chang, pos + 1)
            str_Num = str_Num[:-1]
    else:
        str_Num += str_Num[chang - pos]
        generate_Palind(nums, chang, pos + 1)


for chang in range(minL, maxL + 1):
    generate_Palind(nums, chang, 1)

fout.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值