Q4

问题:

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.

第一次代码:

def pro_palin (n):
    s_str='{0}'.format(n)
    d_str=s_str[::-1]
    sum=0
    n_len=len(s_str)
    for i in d_str :
         sum=sum*10+int(i)
    return n*10**(n_len)+sum
def ismatched (n,index):
    divisor=2
    product=1
    while product!=n:
         print n,product
         if n/(10**(index-1))>0 and  n/(10**(index-1))<10 and product/(10**(index-1))>0 and product/(10**(index-1))<10:
                   print n,product
                   return True 
         while  n%divisor==0:
              if n/(10**(index-1))>0 and  n/(10**(index-1))<10 and product/(10**(index-1))>0 and product/(10**(index-1))<10:
                   print n,product,
                   return True              
              product*=divisor
              n=n/divisor
         divisor+=1
    return False

     
       
          
    
     
############主程序################
if __name__=='__main__': 
    ra=range(900,1000)[::-1]
    #print ra
    """
    for  item in ra :
         
         pralindro=pro_palin(item)
         #print pralindro         
         if ismatched(pralindro,3):
              print item
    """
    print ismatched(9009,2)
    
有问题找不到,99*91所以,我这肯定有问题。

我最终的代码

a

def pro_palin (n):
    s_str='{0}'.format(n)
    d_str=s_str[::-1]
    sum=0
    n_len=len(s_str)
    for i in d_str :
         sum=sum*10+int(i)
    return n*10**(n_len)+sum
def ismatched (n,index):
    divisor=2
    product=1
    ra=range(10**(index-1),10**index)[::-1]
    for i in ra :
         a,remain=divmod(n,i)
         if remain==0 and a/10**(index-1)>0 and a/10**(index-1)<10:
              print i,a
              return True
    return False    
############主程序################
if __name__=='__main__': 
    ra=range(900,1000)[::-1]
    #print ra
    
    for  item in ra :
         
         pralindro=pro_palin(item)
         #print pralindro         
         if ismatched(pralindro,3):
              print pralindro
    
    print ismatched(9009,2)
别人牛人的解答:

The palindrome can be written as:abccbaWhich then simpifies to:100000a + 10000b + 1000c + 100c + 10b + aAnd then:100001a + 10010b + 1100cFactoring out 11, you get:11(9091a + 910b + 100c)So the palindrome must be divisible by 11. Seeing as 11 is prime, at least one of the numbers must be divisible by 11. So brute force in Python, only with less numbers to be checked:

Python
Hide Code
def c():
   max = maxI = maxJ = 0
   i = 999
   j = 990
   while (i > 100):
      j = 990
      while (j > 100):
         product = i * j
         if (product > max):
            productString = str(product)
            if (productString == productString[::-1]):
               max = product
               maxI = i
               maxJ = j
         j -= 11
      i -= 1
   return max, maxI, maxJ
Returns an answer in 0.016 secs.



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值