我试图找到最大的回文数,它是两个 3 位数的乘积。我的客人是回文将有形式abccba
,所以我将遍历每个数字并停在两个 3 位数字乘积的最大数字处。
这段代码
def hasLargeDivisors(n):
"""
Function to determine if a number has two divisors
greater than 99
"""
d = 999
while n / d > 99 and n / d < 999 and d > 99:
if n % d is 0:
return True
d-=1
return False
def compisitePalindrome():
"""
Function to find the largest palindrome
that is the product of 2 three-digit numbers
"""
for a in reversed(xrange(1, 9)):
for b in reversed(xrange(0, 9)):
for c in reversed(xrange(0, 9)):
num = a*100001 + b*10010 + c*1100
if hasLargeDivisors(num):
return num
return 0
产生 888888 = 962 * 924,这是不正确的。
这段代码
def hasLargeDivisors(n):
"""
Function to determine if a number has two divisors
greater than 99
"""
d = 999
while n / d > 99 and n / d < 999 and d > 99:
if n % d is 0:
return True
d-=1
return False
def compisitePalindrome():
"""
Function to find the largest palindrome
that is the product of 2 three-digit numbers
"""
a = 9
for b in reversed(xrange(0, 9)):
for c in reversed(xrange(0, 9)):
num = a*100001 + b*10010 + c*1100
if hasLargeDivisors(num):
return num
return 0
产生 906609 = 993 * 913,这是正确的。
我不知道我哪里错了。