0
$\begingroup$

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

1 Answer 1

4
$\begingroup$

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

$\endgroup$
7
  • $\begingroup$ 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) $\endgroup$ Commented Oct 12, 2013 at 15:49
  • $\begingroup$ 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. $\endgroup$
    – Empy2
    Commented Oct 12, 2013 at 15:51
  • $\begingroup$ @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? $\endgroup$ Commented Oct 12, 2013 at 15:58
  • $\begingroup$ @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? $\endgroup$ Commented Oct 12, 2013 at 16:08
  • $\begingroup$ @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! $\endgroup$ Commented Oct 12, 2013 at 16:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .