python-趣味百题2

1.可逆素数:一个素数的各位数值顺序颠倒后的到的数仍为素数如321,123。找出1-900之间的所有可逆素数

思路:
1).用筛法找到900以内素数表
2).迭代表内所有数,是素数的检测他的反序数是否是素数
3).条件为真,打印这两个素数

def getPrimeTable(n):
    pt = [True] * n
    for p in range(2, n):
        if not pt[p]:continue
        for i in range(p * p, n, p):
            pt[i] = False
               
   return pt
pt = getPrimeTable(900)
for p in range(10,900):
    if not pt[p]:continue
    q = int(str(p)[::-1])
     if p != q < 900 and pt[q]:
            pt[q] = False
            print p, q

原文地址:https://www.cnblogs.com/lucia8522/p/6723633.html