Skip to main content
Code Review

Return to Revisions

3 of 3
added 33 characters in body; edited tags; edited title
toolic
  • 15.1k
  • 5
  • 29
  • 211

Project Euler #4: palindromic number

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.

I'm looking to make this code more efficient, especially the while loop. 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))
lang-py

AltStyle によって変換されたページ (->オリジナル) /