Problem 4 "Maximum Palindrome"
The number of palindromes that have the same value when read from either the left or right is called the number of palindromes. Of the number of palindromes represented by the product of two-digit numbers, the maximum is 9009 = 91 x 99. Then, find the maximum number of palindromes represented by the product of three-digit numbers.
Python
digits = 3
min_num = 10 ** (digits - 1)
max_num = (10 ** digits) - 1
seq = range(min_num ** 2, (max_num ** 2) + 1)
def is_kaibun(x):
x_str = str(x)
x_str_reverse = x_str[::-1]
return x_str == x_str_reverse
def is_same_digits(num1, num2):
return len(str(num1)) == len(str(num2))
result_dic = {}
for x in seq:
if(is_kaibun(x)):
for i in range(min_num, max_num+1):
if x % i == 0 and is_same_digits(i, x/i):
result_dic[x] = (i, x/i)
break
result = max(result_dic.keys())
print result
print result == 906609
print result_dic[result]
result
906609
True
(913, 993)
Recommended Posts