Skip to main content
Code Review

Return to Question

added 33 characters in body; edited tags; edited title
Source Link
toolic
  • 14.6k
  • 5
  • 29
  • 204

Project Euler #4 Python while loop efficiency: 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.

LookingI'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))

Project Euler #4 Python while loop efficiency

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))

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))
added problem desc
Source Link
Daniel
  • 4.6k
  • 2
  • 18
  • 40

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))

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))

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))
Source Link

Project Euler #4 Python while loop efficiency

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))
lang-py

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