[Math] Largest palindrome made from the product of two 3-digit numbers

combinationspalindromeproject euler

I am doing a python problem and I need to find the largest palindrome made from the product of two 3-digit numbers. So, how would I get all possible combinations?

I imagine it is multiplying all combinations of 100 to 999 but am a bit stuck on how to do this. I hope this makes sense.

    def genYieldThreeValues(stop):
    j = 999
    while stop >99 and j > 99:
        multiples = str(stop * j)
        front = multiples[:3] # get front three numbers
        back = multiples[-3:] # get last three numbers
        stop-=1
        j-=1
        yield  [front,back,multiples] # yield a list with first three, last three and all numbers

    def highestPalindrome(n):
    for x in genYieldThreeValues(n):
        if   x[1] ==x[0][::-1]: # compare first three and last three digits reversed
            return x[2]         # if they match return value

    print(highestPalindrome(999))

Best Answer

So you've started Project Euler?

Since you are asked for the largest palindrome from two 3-digit numbers, then why not start two loops and cycle downwards:

for i in range(999,100,-1):
    for j in range(999,100,-1):
       product = i*j

Then you just have to reverse it & compare the product with the reverse. There is an easy way to do this in Python, but part of PE is that you figure it out for yourself :).