Tell me more ×

I want to find the largest palindrome that can be obtained through the multiplication of two 3-digit numbers.

I started off with a and b both being 999, and to decrement a and b with every multiplication that occurred.

a = 999 #Defining Variables
b = 999

for i in range (1000): 
    c= a*b                          #multiply a to b
    if int(str(c)[::-1]) == c:
        print c
    a = a-1                         #decrement the value of a
    c=a*b                           #multiply a by the decremented a
        print c

    b = b-1                         #decrement b so that both a and b have been decremented

The result has turned up 698896, 289982, 94249, 69696... with 698896 being the first number. Currently I'm still trying to figure out what I'm missing.

share|improve this question
Theres no check for whether it is a one or two-digit number. Unless you're considering 010...090 as three-digits (but in this case would need extra code :] ) –  kwah Sep 18 '11 at 9:25
What awful comments! "c= a*b #multiply a to b"? What kind of programmer needs that line explained to them? –  Johnsyweb Sep 18 '11 at 9:26
thank you for focusing on my comments rather than shitty code :P I thought it would at least help to make sense of the abomination I had created. –  WhiteBit Sep 18 '11 at 10:04
Such comments are sometimes useful when you're starting to write code and parts of it are not obvious to you. And they are invaluable to programmers coming from INTERCAL background. –  Rosh Oxymoron Sep 18 '11 at 11:26
@Rosh Oxymoron Thanks for making me look up INTERCAL :D –  Voo Sep 18 '11 at 16:23
show 1 more comment

3 Answers

up vote 8 down vote accepted

You cannot decrement a and b in an alternating fashion, because you're missing value pairs like a = 999 and b = 997 that way.

Try nested looping instead, starting from 999 and counting backwards.

Something like

def is_pal(c):
    return int(str(c)[::-1]) == c

maxpal = 0
for a in range(999, 99, -1):
    for b in range(a, 99, -1):
        prod = a * b
        if is_pal(prod) and prod > maxpal:
            maxpal = prod

print maxpal

EDIT: Modified lower bounds after Paul's comment.

share|improve this answer
range(999,100,-1) does not include 100. Try range(999,99,-1). –  Paul McGuire Sep 18 '11 at 9:40
Paul, 100*999 is less than 698896, though. Also, x*100 ends with 00 for all natural x's, which means there is no palindrome in the set of x*100. –  Emil Vikström Sep 18 '11 at 9:43
@Paul: Nitpicker ;) –  Johannes Charra Sep 18 '11 at 10:04
Also note that we trivially can halve the problem space by only checking for numbers with a >= b. Otherwise we compute both a*b and b*a which is obviously not especially useful. And that we can stop as soon as a*b <= largestPalindrome should be obvious as well - but it's harder to quantify how much that helps –  Voo Sep 18 '11 at 16:28
@Voo: I do only check numbers where a >= b –  Johannes Charra Sep 18 '11 at 16:52
show 2 more comments

You algorithm is wrong. You will need to test all values of a to all values of b, which can be solved by using two loops (the outer for a and the inner for b). I also suggest that you use a and b as loop indices, which simplifies the logic (makes it easier to keep in head).

Consider moving the palindrome check to it's own function as well, to make the code easier to understand.

I'm no Python programmer, but here's my solution in PHP:

function palindrome($x) {
  $x = (string) $x;  //Cast $x to string
  $len = strlen($x); //Length of $x

  //Different splitting depending on even or odd length
  if($len % 2 == 0) {
    list($pre, $suf) = str_split($x, $len/2);
    $pre = substr($x, 0, $len/2);
    $suf = substr($x, $len/2+1);

  return $pre == strrev($suf);

$max = array(0, 0, 0);

//Loop $a from 999 to 100, inclusive.
//Do the same over $b for EVERY $a
for($a = 999; $a >= 100; $a--) {
  for($b = 999; $b >= 100; $b--) {
    $x = $a*$b;

    if(palindrome($x)) {
      echo $a, '*', $b, ' = ', $x, "\n";
      if($x > $max[2]) {
        $max = array($a, $b, $x);
echo "\nLargest result: ", $max[0], '*', $max[1], ' = ', $max[2];
share|improve this answer
add comment

In C# - solution in gist -

public class worker { public worker(){

    public void start()
        int MAX_NUMBER = 999;
        for (int Number = MAX_NUMBER; Number >= 0; Number--)
            string SNumberLeft = Number.ToString();
            string SNumberRight = Reverse(Number.ToString());
            int palindromic = Convert.ToInt32(SNumberLeft + SNumberRight);

            for (int i = MAX_NUMBER; i >= 1; i--)
                for (int l = MAX_NUMBER; l >= 1; l--)
                    if ((i * l) - palindromic == 0)
                        System.Diagnostics.Debug.WriteLine("Result :" + palindromic);

           // System.Diagnostics.Debug.WriteLine( palindromic); 


    public string Reverse(String s)
        char[] arr = s.ToCharArray();
        return new string(arr);

share|improve this answer
add comment

Your Answer


