Find the largest palindrome made from the product of two 3-digit numbers

Problem Statement

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 x 99.

Find the largest palindrome made from the product of two 3-digit numbers.

Notes

This is a relatively simple list comprehension problem. Probably the only interesting aspect here is the way a number is checked for being a palindrome – ie. the test str(x*y) == str(x*y)[::-1]

For testing for palindrome we convert the number into a string. We subsequently reverse the string (the [::-1] slice). If both are same then the underlying number is a palindrome.

Solution

1
2
3
4
print max(x * y
    for x in xrange(100,1000)
        for y in xrange(100,1000)
            if str(x*y) == str(x*y)[::-1])

4 comments

  1. The largest plendrome would be between 200 – 1,998

    It is 1991 which is 995+996!

    I’m right! Again!!!!

    :)

    • Dhananjay Nene

      Perhaps, but the problem statement was slightly different. It is “Find the largest palindrome made from the product of two 3-digit numbers”. Notice that its product, not sum.

  2. 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.

    Some reasoning about the answer: the smallest three digit number is 100, the largest 999. Thus the answer is a five to six digit number from 10,000 to 998,001. As the large number cannot start with 0, it cannot end with 0. It follows that the three digit number cannot end in 0. Neither can they start with 0. Thus, the three digit numbers are therefore [1-9][0-9][1-9], 810 possibilities. It seems simplest to iterate over the three digit numbers:

    max = None
    for i in range (1, 10):
    for j in range (0, 10):
    for k in range (1, 10):
    n = (i * 100) + (j * 10) + k
    s = str (n**2)
    midpt = len (s) / 2
    if (s[:midpt] == s[-midpt:][::-1]):
    max = s
    print max

    The answer is – the suprisingly low – 698896

    836 x 836 = 698,896

    Haha! I’m right! Again!!!!!

    ;)