我是编程新手。我正在解决Project Euler Question 12:
The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
Let us list the factors of the first seven triangle numbers:
1: 1 3: 1,3 6: 1,2,3,6 10: 1,2,5,10 15: 1,3,5,15 21: 1,3,7,21 28: 1,2,4,7,14,28 We can see that 28 is the first triangle number to have over five divisors.
What is the value of the first triangle number to have over five hundred divisors?
我的解决方案是:
import math
k=0
count=0
for i in range (1,100000):
k+=i
a=int(math.sqrt(k))+1
count=0
for j in range (1,a):
n=a%j
if n==0:
count=count+1
count*=2
if count>500:
print'',count
break
对于count>100
和小于100的数字,它会很快给出输出,
但对于500人来说,这需要很长时间。如何缩短执行时间?你知道吗
只能使用奇数。你知道吗
应该快一倍。你知道吗
您还可以将所有已知的素数保存到一个列表中,并且只通过该列表以%进行测试。你知道吗
相关问题 更多 >
编程相关推荐