2
\$\begingroup\$

The problem statement for Project Euler Problem 4 is as follows:

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers.

Looking to make this code more efficient. I'm using a variable to pass into the function so that the answer to ints of various lengths can be determined. It's been the most efficient that I've been able to come up with. Any suggestions?

def largestPalindromeProduct(digits):
    num=int(digits*"9")
    maxPalindrome=0
    i=int(num)
    floor=int(num/10) if digits > 1 else -1
    while i > floor:
        j=i
        while j > floor:
            val=str(i*j)
            if val==val[::-1]:
                floor=j
                maxPalindrome=max(maxPalindrome,i*j)
            j-=1
        i-=1
    return maxPalindrome

print(largestPalindromeProduct(3))
\$\endgroup\$
  • \$\begingroup\$ @stefan Comments are for seeking clarification to the question, and may be deleted. Please put all suggestions for improvements in answers, even if they are brief. \$\endgroup\$ – 200_success Feb 16 '18 at 21:57
  • \$\begingroup\$ I left a comment here with a link to an answer for the eactly same problem. This was deleted. Maybe by @200_success, but I can't be for sure. \$\endgroup\$ – stefan Feb 17 '18 at 9:21
  • \$\begingroup\$ @stefan You can put that same remark and link in an answer. \$\endgroup\$ – 200_success Feb 17 '18 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.