`
诗意的栖居
  • 浏览: 268045 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

No.4 The largest palindrome made from the product of two 3-digit numbers

 
阅读更多
Q:
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.

A:
''' method 1
    The palindrome can be written as: abccba Which then simpifies to: 100000a + 10000b + 1000c + 100c + 10b + a And then: 100001a + 10010b + 1100c Factoring out 11, you get: 11(9091a + 910b + 100c)
    所以一个乘数一定是11的倍数
'''

i= 999
maxNum = 0
while i > 99:
    j = 990
    while j > 99:
        re = i * j
        l = str(re)
        if l == l[::-1]:
            if int(l) > maxNum:
                maxNum = int(l)
        j -= 11
    i -= 1

print "palindrome is %d" %maxNum

'''method 2
   强大的精简
'''

max([x*y for x in range(900,1000) for y in range(900,1000) if str(x*y) == str(x*y)[::-1]])

pailndrome is 906609
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics