Project Euler: Problem 4

https://projecteuler.net/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.

I made x and y the range of 3 digit numbers, then iterated over the product of all x,y and called that result product. The trick I used was that if a product is a palindrome then product subtraced by the reverse of product will be equal to zero. I turned product into a string in order to find the reverse of product by using slice. The -1 in slice means the steps are in the negative direction. If ever product is a palindrome and is bigger than the previous palindrome I found, I renamed my Largest variable to be product.


In [12]:
Largest = 0
for x in range(100,1001):
    for y in range(100,1001):
        product = str(x * y)
        if int(product) - int(product[::-1]) == 0 and int(product) > Largest:
            Largest = int(product)
print("Answer: " + str(Largest))


Answer: 906609

In [ ]:
# This cell will be used for grading, leave it at the end of the notebook.