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.


In [3]:
def Is_Palindrome(x):
    list_x = list(str(x))
    for n in range(0,len(list_x)):
        if list_x[n] != list_x[len(list_x) - n - 1]:
            return False
            break
    return True

In [4]:
print(list(str(5005)))


['5', '0', '0', '5']

In [5]:
Is_Palindrome(10033001)


Out[5]:
True

In [6]:
def Largest_Palindrome_3digits():
    candidate_list = []
    #first test only the top 10
    for n1 in range(990,999):
        for n2 in range(990,999):
            if Is_Palindrome(n1*n2):
                candidate_list.append(n1*n2)
    if len(candidate_list) > 0:
        return max(candidate_list) + " is a product of " + str(n1) + " and " + str(n2)
    else:
    #if no palindromes are found in the last 10, test the last 100
        for n1 in range(900,999):
            for n2 in range(900,999):
                if Is_Palindrome(n1*n2):
                    candidate_list.append(n1*n2)
        if len(candidate_list) > 0:
            return str(max(candidate_list)) + " is a product of " + str(n1) + " and " + str(n2)
                
    #I could continue this pattern down to the full range, but I don't need to in this case

While the palindrome is correct, the factors are not. The problem doesnt need them, so you can just ignore them.


In [7]:
print(Largest_Palindrome_3digits())


906609 is a product of 998 and 998

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