为什么下面的代码停在回文值:580085,而问题的正确答案是906609这个乘法的结果:993*913
#!/usr/bin/env python
# Find the largest palindrome made from the product of two 3-digit numbers.
# Reverse algorithm
def flipNumber( number ):
nmbList = list(str(number))
i = len( nmbList )
reverseList = list()
# reverse the list
for x in nmbList:
reverseList.append( nmbList[i-1] )
i -= 1
# join list items to one number & cast it to integer
return int(''.join(reverseList))
# Figure out if numbers are palindrome
def isPalindrome( number ):
return number == flipNumber( number )
def multiplyRun( ):
for i in range( 100, 999 ):
for y in range( 100, 999 ):
if isPalindrome( i * y ):
maxPalind = i * y
return maxPalind
# simple tests
# print( flipNumber( 90819 ) )
# print( isPalindrum( 9009, 9009 ) )
# print( isPalindrum( 9109, 9009 ) )
# print( isPalindrum( 9009, 9019 ) )
# print( isPalindrum( 9019, 9019 ) )
# print( isPalindrum( 9119, 9119 ) )
print(multiplyRun( ))
如果我在第二个 for 循环中将第二个 100 更改为 600(或更高),我会得到正确的结果。真是令人难以置信,因为这些嵌套循环应该跨越所有范围......我做错了什么!?(可能是我没有看到的非常愚蠢的东西..)