This one works:
i = 0
j = 0
pol = 0
while i <= 999:
j = i
while j <= 999:
temp = str(i * j)
tempInverso = temp[::-1]
if temp == tempInverso:
polTemp = int(temp)
if polTemp > pol:
pol = polTemp
j += 1
i += 1
print(pol)
Note that there are two links here. The reason is because what you did, you had i * 999
, that is, you were looking for the largest multiple of 999 that was palindrome, not the largest product of two numbers up to three digits that was palyndrome.
To do with two numbers, use i * j
, where I use a loop inside the other, one going through the values of i
and another the values of j
. The j
value is initialized with j = i
instead of j = 0
because since multiplication is commutative, j
values that are less than i
are repeated and therefore unnecessary to be tested.
Another problem that you had is that when you find a palindrome, you already assign it to the variable pol
, without checking if this palindrome is better than the previous one already found. As a result, it would show the last palyndrome found, not the largest palyndrome. The solution to this was to use a polTemp
variable and only assign it to pol
and polTemp > pol
.
See here working on ideone.