这是我的实现,但在给定6位数字时效率不高。你知道吗
Input : n = 2
Output : 9009
9009是最大的数,是两个数的乘积
两位数。9009 = 91*99. 你知道吗
def isPali(x):
n = str(x)
for i in range(len(n)):
if not n[i] == n[-i-1]:
return False
return True
def isProduct(x,A):
counter = A
while counter > 1:
if x // counter <= A and x % counter == 0:
return True
else:
counter-=1
return False
def largestProduct(A):
for i in range(A*A,1,-1):
if isPali(i) and isProduct(i,A):
return i
return False
largestProduct(999999)
设x和y是回文数的两个n位因子。你知道吗
您可以按降序数对它们进行迭代。你知道吗
关键是要尽快停止,也就是说,一旦找到第一个解决方案,就不要检查低于该解决方案的任何产品。你知道吗
在我的笔记本电脑上执行0.378秒。你知道吗
代码方面,这并不太难:
然而,这是一种暴力手段。对于6位数字,这不会在合理的时间内终止。即使会,我也可以问你同样的问题,7或42位数字。我建议你寻找一些结构,或属性,这些数字的倍数是一个回文。这样的一对可能是任何一对数字吗?91*99=9009是巧合还是有规律?你知道吗
相关问题 更多 >
编程相关推荐