06.02.08

Project Euler: Problem 4

Posted in python tagged , , , at 1:18 pm by redochre

Problem 4:

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.

A solution:

biggest_palindrome = 0

for i in range(100,1000):
    for j in range(100,1000):
        num = i*j
        if int(str(num)[::-1]) == num and num > biggest_palindrome:
            biggest_palindrome = num

print "biggest palindrome is %d" %  biggest_palindrome  

Pretty straightforward, brute-force way to do it. Though knowing how to reverse a string (with str(a)[::-1]) is a neat trick.

Leave a Comment