# Largest palindrome made from the product of two 3-digit numbers

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))

• This is Project Euler, number 4 Commented May 7, 2014 at 20:16
• You might consider generating palindromes and then factoring.
– Zook
Commented May 7, 2014 at 20:25
• @Zook I did this in October '13! Commented May 7, 2014 at 20:27
• ah, looks like @Ross just raised it from the dead.
– Zook
Commented May 7, 2014 at 20:28
• Yes, I was a bit lost as to the comment also as it is stated in the answer that it is for Euler. Commented May 7, 2014 at 20:29

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 :).

• I have indeed, I was trying to do it using a generator so it could be used on very large numbers, I am a bit lost on the maths behind getting the combinations, getting late and I have been staring at this for probably too long!(added my code to show where I am lost) Commented Oct 12, 2013 at 15:49
• If it is going too slow, remember that all palindromes with an even number of digits have a common factor, so either $i$ or $j$ is a multiple of that. Commented Oct 12, 2013 at 15:51
• @Michael do i need to calculate every possible product to get the answer? If so in mathematical terms how do you get every possible outcome of multiplying two sets of numbers from 100 to 999? Commented Oct 12, 2013 at 15:58
• @PadraicCunningham: Once you have found one that works, you don't need to check any smaller numbers. That is why Kyle suggested starting from the top. If you were to discover that 918819 were the product of two three digit numbers (it is not), you wouldn't have to check factors smaller than what? Commented Oct 12, 2013 at 16:08
• @ron by starting at 999 in my code am I not starting at the higher possible values, I think I must be over complicating this in my head! Commented Oct 12, 2013 at 16:11