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))
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]:
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]:
with number of palindroms
In [14]:
sum( 1 for i,j in combinations(range(100, 1000),2) if is_palindrom(i*j) )
Out[14]:
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 [ ]: