Q) 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.
Solution: Python
============= def isPalindrome(x): if len(x)<=1: return True else: return (x[0] == x[-1]) and isPalindrome(x[1:-1]) db = [0] def TestPalindrome(): x = 1 y = 1 for i in range(999,100,-1): for j in range(999,100,-1): ans = str(i*j) if isPalindrome(ans): db.append(int(ans)) x,y = i,j break elif max(i,j)<min(x,y): return max(db) print TestPalindrome()
Find the largest palindrome made from the product of two 3-digit numbers.
Solution: Python
============= def isPalindrome(x): if len(x)<=1: return True else: return (x[0] == x[-1]) and isPalindrome(x[1:-1]) db = [0] def TestPalindrome(): x = 1 y = 1 for i in range(999,100,-1): for j in range(999,100,-1): ans = str(i*j) if isPalindrome(ans): db.append(int(ans)) x,y = i,j break elif max(i,j)<min(x,y): return max(db) print TestPalindrome()
No comments:
Post a Comment