Thứ Bảy, 16 tháng 4, 2016

[#4] Largest palindrome product

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 ispalindromic(n):
        '''
        :param n: int need to check
        :return: True or False
        '''
        a = list(str(n))
        a.reverse()
        return True if a == list(str(n)) else False
def problem4(n):
        '''
        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 n-digit numbers.
        '''
        a = int('9' * n)#the largest n-digit number
        result = 0
        for i in range(a, int('9' * (n - 1)), -1):
            for j in range(a, int('9' * (n - 1)), -1):
                if ispalindromic(i * j):
                    if i * j > result:
                        result = i * j
        return result

ChiefdaThief

Author & Editor

Keep It Simple Stupid.

0 nhận xét:

Đăng nhận xét

Manual Categories