highest palindrome with 3 digit numbers in python

后端 未结 13 1230
梦如初夏
梦如初夏 2021-02-01 10:35

In problem 4 from http://projecteuler.net/ it says:

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-d

13条回答
  •  天涯浪人
    2021-02-01 11:25

    Here I added two 'break' to improve the speed of this program.

    def is_palindrome(num):
        return str(num) == str(num)[::-1]
    def max_palindrome(n):
        max_palindrome = 1
        for i in range(10**n-1,10**(n-1)-1,-1):
            for j in range(10**n-1,i-1,-1):
                if is_palindrome(i*j) and i*j > max_palindrome:
                    max_palindrome = i * j
                    break
                elif i*j < max_palindrome:
                    break
        return max_palindrome
    n=int(raw_input())
    print max_palindrome(n)
    

提交回复
热议问题