In [1]:
"""
Problem 4
=========
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.
"""
import itertools
def isPalindrome(x):
"""Checks if x is a palindrome. Returns true or false."""
return str(x) == str(x)[::-1]
"""
This is my old solution. I'm sorry.
a = str(x)
l = len(a)
hl = l/2
i = 0
if l%2 ==0:
# print("even")
while (i < hl):
# print(i)
# print(l-i-1)
if a[i] != a[l-i-1]:
# print("not palindrome")
return(0)
# break
i = i+1
return(1)
else:
# print("odd")
while (i < hl):
# print(i)
# print(l-i-1)
if a[i] != a[l-i-1]:
# print("not palindrome")
return(0)
# break
i = i+1
return(1)
"""
def findMaxPalindrome(maxNum):
"""Returns highest palindrome from all possible products of numbers up to maxNum."""
combinations = itertools.combinations(range(maxNum),2) # all possible multiplier combinations within range
products = []
for y in combinations:
prod = y[0]*y[1]
if(isPalindrome(prod)):
products.append(prod) # all possible products in range
return(max(products))
In [2]:
findMaxPalindrome(999)
Out[2]:
In [ ]: