我用Python开发了一个针对Euler问题12的程序。此代码将三角形数除以找到可以被除以500的最小三角形数。我的代码可以运行,但是速度很慢,并且花费不合理的时间来得出答案。我是否可以进行一些小的改进以提高性能,还是需要以其他方法完全重新开始?谢谢!
# Project Euler Problem #12
# Highly divisible Triangle Number
# What is the value of the first triangle number to have over five hundred divisors?
# Variables
add = 0
i = 0
divisors = [0]
def multiple_counter(n):
multiples = []
for i in range(1,n+1):
if n % i == 0:
multiples.append(i)
return len(multiples)
while max(set(divisors)) <= 499:
i += 1
add += i
divisors = []
divisors.append(multiple_counter(add))
print( max(set(divisors)), add, i) # this is my debugger
print (add)