Find the largest palindrome made from the product of two 3-digit numbers - Python

Asked

Viewed 1,475 times

1

I’m having a problem. I’m creating a Python algorithm to find the largest palindrome made from the product of two 3-digit numbers.

Code:

i = 0
while i <= 999:
    temp = str(i * 999)
    tempInverso = temp[::-1]
    if temp == tempInverso:
        pol = int(temp)    
    i += 1
print(pol)

It returns me the value 90909, but the correct value is 906609. Since I can’t find anything wrong with the code, I’ve come for your help. I thank you all for your attention.

2 answers

3


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 loops 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, and not the largest product of two numbers up to three digits that was palindrome.

To do with two numbers, use i * j, where I use a loop inside the other, one traversing the values of i and other the values of j. The value of j is initialized with j = i instead of j = 0 because once multiplication is commutative, the values of j who are less than i are repeated and therefore unnecessary to be tested.

Another problem you had is that when you found a palindrome, you already assigned 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 found palindrome, not the largest palindrome. The solution to this was to use a variable polTemp and only attribute it to pol if polTemp > pol.

See here working on ideone.

  • It worked fine thanks!

  • Now that you commented I realized the bullshit I was doing searching only in the multiples of 999.

  • The accountants i and j may start at 100, since only the product of 3-digit numbers is to be considered.

  • @Brunosantos If this answer solved your problem and there was no doubt left, I ask you to mark it as accepted/correct by clicking below the vote number.

  • @Andersoncarloswoss This is debatable, since 012 may or may not be considered a three-digit number.

  • I disagree. Mathematically speaking even the value 012 has two digits. We are working with numbers, not strings.

  • @Andersoncarloswoss That’s why I said it’s debatable, it can be argued both against and for and depends on the desired context. However, this program works independently of what interpretation is given for "three-digit number".

Show 2 more comments

2

Another possible solution:

from itertools import product

palindromes = (i*j for i, j in product(range(100, 1000), repeat=2) if str(i*j) == str(i*j)[::-1])

print("O maior palíndromo encontrado foi", max(palindromes))

See working on Ideone.

The function product(range(100, 1000), repeat=2) will generate all combinations of values from two to two between 100 and 999. A repeat loop is made over this list, storing the first value in i and the second in j. The value is returned to the final list i*j if the condition str(i*j) == str(i*j)[::-1] is true. With that, palindromes will be a Generator with all palindromes product result between two three-digit values. With function max we get the biggest of them.

  • Thank you for your help !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.