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.

First, I defined two lists which hold the values of all three digit numbers


In [32]:
a = range(100, 1000)
b = range(100, 1000)

Then I created an empty list, which would hold all the palindromes created from multiplying those three digit numbers.


In [33]:
lst = []

Using two for loops, I looped through all the values in a, multiplying them by all the possible values of b, and calling the product p. Using an if statement, I turned the product into a string and compared whether it was the same fowards as backwards. If it was, I appended it to my list of palindromes.


In [34]:
for x in a:
    for y in b:
        p = x * y
        if str(p) == str(p)[::-1]:
            lst.append(p)

Finally, I printed the maximum palindrome using the built in max function.


In [35]:
print(max(lst))


906609

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