def IsPalin(string):
start, end = 0, len(string) - 1
while end > start:
if string[start] != string[end]:
return False
start += 1
end -= 1
return True
div = [x for x in range(101, 999) if (x % 11 == 0 and x % 10 != 0)]
i = len(div) - 1
factor = 999
ret = False
max = 0
while i > 0:
while factor > 100:
if IsPalin(str(factor*div[i])):
print factor*div[i]
if max < factor*div[i]:max = factor*div[i]
# ret = True
break
factor -= 1
# if ret:break
factor = 999
i -= 1
print max
参考了另一个人的推理过程,简化了计算:http://pthree.org/2007/09/15/largest-palindromic-number-in-python/
Let's look at the numeric column of each letter:
a(100,000) + b(10,000) + c(1,000) + c(100) + b(10) + a(1)
Adding similar variables (in algebraic notation):
100001a + 10010b + 1100c
The common denominator is 11:
11(9091a + 910b + 100c)
This means that each palindrome will be a multiple of 11.
在我看来都是高中的知识,but自己忘了,都他娘的忘了。。。,不过这个人的解法也有点问题,后面的评论也有人指出:
Good post…only a doubt.
When you reach the column of the table of multiple of 11 (990,979,etc.) i think you must multiplicate this number for all the numbers smaller than this, and not only for multiple of 11 smaller than this. For example I don’t see 990*989,990*988,etc. that are multiple of 11 too. Don’t you think?