projecteuler网站第四题解法:Find the largest palindrome made from the product of two 3-digit numbers

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自己忘了,都他娘的忘了。。。,不过这个人的解法也有点问题,后面的评论也有人指出:


  1. Duccio using Konqueror 3.5 Konqueror 3.5 on Kubuntu Kubuntu | September 16, 2007 at 12:38 am | Permalink

    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?


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值