• Project Euler Problem4


    Largest palindrome product

    Problem 4

    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.

    The python code is as follows:

    def isPalindromic(data):
        list = []
        while data > 10:
            list.append(data%10)
            data = int(data/10)
        list.append(data)
        print(list)
        i = 0
        j = len(list)-1
        while i < j:
            if list[i] != list[j]:
                return False
            i += 1
            j -= 1
        return True
        
    a = 999
    total = 0
    result = 0
    targetA = 0
    targetB = 0
    while a >= 100:
        b = 999
        if a*b < result:
            break
        while b >= 100:
            total = a*b
            if total < result:
                break
            if isPalindromic(total):
                result = total
                targetA = a
                targetB = b
            b -= 1
        a -= 1
        
    print(result)
    print(targetA)
    print(targetB)
    

      Assuming b is large than a, we can rewrite the code like this:

    def isPalindromic(data):
        list = []
        while data > 10:
            list.append(data%10)
            data = int(data/10)
        list.append(data)
        print(list)
        i = 0
        j = len(list)-1
        while i < j:
            if list[i] != list[j]:
                return False
            i += 1
            j -= 1
        return True
        
    a = 999
    total = 0
    result = 0
    targetA = 0
    targetB = 0
    while a >= 100:
        b = 999
        if a*b < result:
            break
        while b >= a:
            total = a*b
            if total < result:
                break
            if isPalindromic(total):
                result = total
                targetA = a
                targetB = b
            b -= 1
        a -= 1
        
    print(result)
    print(targetA)
    print(targetB)
    

      

  • 相关阅读:
    兼容性问题
    Webfont 的兼容性问题[持续更新]
    WebView的坑[持续更新]
    [转]60fps on the mobile web
    IT男送什么礼物给女朋友呢?
    Lumia 1520 IE mobile window.devicePixelRatio
    Fiddler 故障
    IE(8~11+) 可用右键加速器
    [转]Zen Cart官网屏蔽中国用户访问的真正原因
    Internet Explorer Developer Channel 自动化测试 IE 浏览器
  • 原文地址:https://www.cnblogs.com/tianxiaozz/p/3471321.html
Copyright © 2020-2023  润新知