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 [1]:
def is_palindrome(possible): #tests if arguement is a palindrome
    front=str(possible) #converts arguement to string
    back=front[::-1] #sets back to a reverse of front
    if front==back: 
        return front  #if the two are equal returns original

In [8]:
def largest_palindrome(start,end):
    if start >=100 and start <=999 and end >=100 and end <=999: #tests to see that palindrome will have factors of three digits
        prev=0            #assigns prev a value
        for x in range(start,end): # iterates through x 
            for y in range(start,end): #for every x, iterates through y
                if is_palindrome(x*y): # tests to see if product of two is a palindrome
                    if x*y>prev:    #since searching for greatest palindrome, only reassings prev if greater 
                        prev=x*y
    return prev   #returns the greatest palindrome

In [11]:
print(largest_palindrome(100,999))


906609

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