Problem 55
If we take 47, reverse and add, 47 + 74 = 121, which is palindromic.
Not all numbers produce palindromes so quickly. For example,
349 + 943 = 1292
1292 + 2921 = 4213
4213 + 3124 = 7337
That is, 349 took three iterations to arrive at a palindrome.
Although no one has proved it yet, it is thought that some numbers, like 196, never produce a palindrome. A number that never forms a palindrome through the reverse and add process is called a Lychrel number. Due to the theoretical nature of these numbers, and for the purpose of this problem, we shall assume that a number is Lychrel until proven otherwise. In addition you are given that for every number below ten-thousand, it will either (i) become a palindrome in less than fifty iterations, or, (ii) no one, with all the computing power that exists, has managed so far to map it to a palindrome. In fact, 10677 is the first number to be shown to require over fifty iterations before producing a palindrome:
4668731596684224866951378664 (53 iterations, 28-digits).
Surprisingly, there are palindromic numbers that are themselves Lychrel numbers; the first example is 4994.
How many Lychrel numbers are there below ten-thousand?
NOTE: Wording was modified slightly on 24 April 2007 to emphasise the theoretical nature of Lychrel numbers.
将47倒序并相加得到47 + 74 = 121,是一个回文数。
不是所有的数都能像这样迅速地变成回文数。例如,
349 + 943 = 1292
1292 + 2921 = 4213
4213 + 3124 = 7337
也就是说,349需要迭代三次才能变成回文数。
尽管尚未被证实,但有些数,例如196,被认为永远不可能变成回文数。如果一个数永远不可能通过倒序并相加变成回文数,就被称为利克瑞尔数。出于理论的限制和问题的要求,在未被证否之前,我们姑且就认为这些数确实是利克瑞尔数。除此之外,已知对于任意一个小于一万的数,它要么在迭代50次以内变成回文数,要么就是没有人能够利用现今所有的计算能力将其迭代变成回文数。事实上,10677是第一个需要超过50次迭代变成回文数的数,这个回文数是
4668731596684224866951378664(53次迭代,28位数)。
令人惊讶的是,有些回文数本身也是利克瑞尔数数;第一个例子是4994。
小于一万的数中有多少利克瑞尔数?
注意:2007年4月24日,题目略作修改,以强调目前利克瑞尔数理论的限制。
def palindr(n):
string = str(n)
string1 = string[::-1]
if string == string1:
return True
else:
return False
def rev_add(n):
string = str(n)
string1 = string[::-1]
return int(string) + int(string1)
def iteration(n, it=0):
if it == 50:
return 1
if palindr(n):
return 0
else:
return iteration(rev_add(n), it + 1)
def main():
sum1 = 0
for x in range(1, 10000):
sum1 += iteration(rev_add(x)) # 每个数至少反转一次
print(sum1)
main()