我们将47与它的逆转相加,47 + 74 = 121, 可以得到一个回文。
并不是所有数都能这么快产生回文,例如:
349 + 943 = 1292,
1292 + 2921 = 4213
4213 + 3124 = 7337
也就是说349需要三次迭代才能产生一个回文。
虽然还没有被证明,人们认为一些数字永远不会产生回文,例如196。
那些永远不能通过上面的方法(逆转然后相加)产生回文的数字叫做Lychrel数。
因为这些数字的理论本质,同时也为了这道题,我们认为一个数如果不能被证明的不是Lychrel数的话,那么它就是Lychre数。
此外,对于每个一万以下的数字,你还有以下已知条件:
这个数如果不能在50次迭代以内得到一个回文,那么就算用尽现有的所有运算能力也永远不会得到。
10677是第一个需要50次以上迭代得到回文的数,它可以通过53次迭代得到一个28位的回文:4668731596684224866951378664。
令人惊奇的是,有一些回文数本身也是Lychrel数,第一个例子是4994。
10000以下一共有多少个Lychrel数?
def is_palindrome(x):
""" 判断x是否回文 """
x_str = str(x)
x_len = len(x_str)
if x_str[:x_len // 2] == x_str[-1:-(x_len // 2 + 1):-1]:
return True
else:
return False
return True
# 直接计算
Lychrel_num = 0
for i in range(10000):
j = i
for _ in range(50):
k = int(str(j)[::-1])
j += k
if is_palindrome(j):
break
if not is_palindrome(j):
Lychrel_num += 1
print(Lychrel_num)
# 引入not_Lychrel标志,缓存计算过的数据
# 如判断349后,943、1292、2921、4213、3124无需判断
not_Lychrel = [False] * 10000
Lychrel_num = 0
for i in range(10000):
if not_Lychrel[i]:
continue
j = i
l = []
for _ in range(50):
k = int(str(j)[::-1])
l.append(k)
j += k
l.append(j)
if is_palindrome(j):
break
if is_palindrome(j):
for l_ in l[:-1]:
if l_ < 10000:
not_Lychrel[l_] = True
else:
Lychrel_num += 1
print(Lychrel_num)