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.

Create a way to test for palindrom:


In [11]:
def is_palindrom(content):
    c=str(content)
    return c == c[::-1]

print(9009, is_palindrom(9009))
print(100, is_palindrom(100))


9009 True
100 False

Generate all products of numbers containing 3 digits and keep the max of this:


In [12]:
from itertools import combinations
max((i*j, i,j) for i,j in combinations(range(100, 1000),2) if is_palindrom(i*j) )


Out[12]:
(906609, 913, 993)

and the smallest palindrom:


In [13]:
min((i*j, i,j) for i,j in combinations(range(100, 1000),2) if is_palindrom(i*j) )


Out[13]:
(11211, 101, 111)

with number of palindroms


In [14]:
sum( 1 for i,j in combinations(range(100, 1000),2) if is_palindrom(i*j) )


Out[14]:
1231

visualized


In [55]:
from itertools import permutations
all_pall = [(i*j, i,j) for i,j in permutations(range(100, 1000),2) if is_palindrom(i*j)]
s, x,y = zip(*all_pall)
import matplotlib
%matplotlib inline
matplotlib.pyplot.scatter(x,y,c=s)
matplotlib.pyplot.gray()
matplotlib.pyplot.show()


where are the palindroms


In [53]:
n, bins, patches = plt.hist(s, 200, facecolor='green', alpha=1)



In [ ]: