Project Euler Problem 4

A palindromic number reads the same both ways.

Project Euler Problem 4

Solution

We seek the largest palindrome that is the product of two 3-digit numbers.

Mathematical approach

A palindrome reads the same forwards and backwards. We can brute-force all products of two 3-digit numbers (from 100 to 999), check whether the product is a palindrome, and track the maximum.

A number is a palindrome if its string representation equals its reverse.

Python code

largest = 0

for i in range(100, 1000):
    for j in range(100, 1000):
        product = i * j
        if str(product) == str(product)[::-1]:
            largest = max(largest, product)

print(largest)

Mental trace / correctness check

We test all possible products of 3-digit numbers. The known maximum palindrome from this search is:

$$993 \times 913 = 906609$$

Check palindrome:

  • 906609 reversed is 906609 → valid palindrome.

No larger 3-digit product palindrome exists, since exhaustive search covers all possibilities.

Answer: 906609