[b]E004[/b]
[i]A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.
Find the largest palindrome made from the product of two 3-digit numbers.
水仙花数是从左向右和从右向左读都相同的自然数。求两个三位数的乘积中最大的水仙花数。[/i]
考虑两个三位数乘积最大的是 999x999, (平均数999)
其次是 998x999, (平均数998.5)
再次是 998x998, 997x999, (平均数998)
然后是 997x998, 996x999, (平均数997.5)
......
[i]A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.
Find the largest palindrome made from the product of two 3-digit numbers.
水仙花数是从左向右和从右向左读都相同的自然数。求两个三位数的乘积中最大的水仙花数。[/i]
def problem4():
min, max = 100, 999
def isPalindromic(num):
if num<=10:
return False
digits = list(str(num))
revdigs = list(str(num))
revdigs.reverse()
return digits==revdigs
x = y = avg = max
while avg>=min:
num = x*y
if isPalindromic(num):
return x,y,num
elif x<=min or y>=max:
avg -= 0.5
x = int(avg)
y = int(avg+0.5)
else:
x -= 1
y += 1
if __name__=='__main__':
print str(problem4())
考虑两个三位数乘积最大的是 999x999, (平均数999)
其次是 998x999, (平均数998.5)
再次是 998x998, 997x999, (平均数998)
然后是 997x998, 996x999, (平均数997.5)
......