/code/blog

Code, code and code

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])

Comments