"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."
My approach to this problem is to do the multiplication, put the product into a string, reverse the string and compare the reversed string to the original string. Every time the comparison is true, I'll save the result as a variable.
For this problem I'm going to use two for-loops, one for each factor. I also need to find a way to reverse a string in Python. Google! Oh, when googling "python palindrome", I found on Stack overflow that there is actually a very compact way in Python to check for palindromes.
Wow, this was really easy thanks to the str(x*y)[::-1]
7 rows!
/Ludvig
My approach to this problem is to do the multiplication, put the product into a string, reverse the string and compare the reversed string to the original string. Every time the comparison is true, I'll save the result as a variable.
For this problem I'm going to use two for-loops, one for each factor. I also need to find a way to reverse a string in Python. Google! Oh, when googling "python palindrome", I found on Stack overflow that there is actually a very compact way in Python to check for palindromes.
Wow, this was really easy thanks to the str(x*y)[::-1]
largestPalindrome = 0for x in range(100,1000): for y in range(100,1000): if str(x*y) == str(x*y)[::-1]: if x*y>largestPalindrome: largestPalindrome = x*y print(largestPalindrome)
7 rows!
/Ludvig
No comments:
Post a Comment