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.
brute force.
the key problem is to check if a number is palindromic.
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.
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
is_palindromic(9009)
is_palindromic(123)
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
solve()