[Tutor] largest palindrome number

Peter Otten __peter__ at web.de
Sat Aug 27 17:48:16 CEST 2011


surya k wrote:

> If you take a close look at my code.
> 
> for i in range (1,100) :
>     for j in range (i,100) :
>        Temp = palindrome(i*j)
> 
> here, as the loop goes on, i*j can never become smaller in any case.
> which is why I think it, as long as "PNum" gets a new number, its
> bigger palindrome than the previous.. so, at the end of the loop.
> we'll get largest palindrome number..

Add a print statement to your loop

for i in range(1, 1000):
    for j in range(i, 1000):
        temp = palindrome(i*j)
        if temp != 0:
            print temp, i, j
            pnum = temp
print pnum

and you'll see the problem:

...
828828 897 924
819918 902 909
824428 902 914
906609 913 993 <--
886688 916 968
861168 924 932
888888 924 962
888888

You get the palindrome with the largest factor i which is not necessarily 
the one with the largest product i*j.



More information about the Tutor mailing list