原题地址:http://projecteuler.net/problem=4
Largest palindrome product
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.
大意是:
一个回文数指的是从左向右和从右向左读都一样的数字。最大的由两个两位数乘积构成的回文数是9009 = 91 * 99.
找出最大的有由个三位数乘积构成的回文数。
解法1:
从两个最大三位数之积开始,如果是回文数,则看看能不能分解成两个三位数之积,如果能则跳出循环。
python代码如下所示:
def isHuiWen(number):
return number == number[::-1]
topNumber = 999*999
max = 999
min = 100
flag = False
while topNumber > 1E2:
if isHuiWen(str(topNumber)):
for i in range(max,min-1,-1):
if topNumber%i == 0 and \
topNumber//i <= max:
flag = True
break
if flag:
break
topNumber = topNumber-1
print i
print topNumber
注:题目的中文翻译源自http://pe.spiritzhang.com