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 [4]:
n = 0
x = 100
y = 100
z = 0
while x < 1000: #Cycles through all of x up to 1000 (3 digit numbers)
    x = x + 1
    y = 100
    while y < 1000: #Cycles through all of y up to 1000 for each x (Not very efficient but it works in this application)
        y = y + 1
        z = y * x
        def check_palindrome(z): #This function takes the string of the product of x and y and splits it into two halves
            pal = str(z)
            leng = len(pal)
            half = int(leng/2)
            first = pal[0:int(half)] #The use of int() and round() Are used so the first half of the palindrome and the second half are the same length.
            last = pal[leng:round(half-.5):-1] #Noah Miller told me that you can use the -1 to go backwards
            if first == last: #If the halves match it is a palindrome
                return True
            else:
                return False
        if check_palindrome(z) == True and z > n: #The palindrome replaces an integer n only if the palindrome is larger
            n = z
print (n)


906609

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