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.


Idea

brute force.
the key problem is to check if a number is palindromic.

Possible enhancement

The most significant digit of the largest palindrome must be 9, so the least significant digit shall be 9. So the least signicant digit of its factors must be 1, 3, 7 or 9.


In [1]:
def is_palindromic(number):
    test_number = number
    reversed_number = 0
    while number:
        reversed_number = reversed_number * 10 + number % 10
        number //= 10
    return reversed_number == test_number
In [2]:
is_palindromic(9009)
Out[2]:
True
In [3]:
is_palindromic(123)
Out[3]:
False
In [4]:
def solve():
    largest_palindrome = (0, (0, 0))
    for i in range(999, 900, -1):
        for j in range(i, 900, -1):
            if is_palindromic(i * j):
                largest_palindrome = max(largest_palindrome, (i * j, (i, j)), key=lambda p: p[0])
    return largest_palindrome
In [5]:
solve()
Out[5]:
(906609, (993, 913))