title:

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?

翻译:

「三角数」即用递增的自然数相加得到的数。因此第7个三角数为1 + 2 + 3 + 4 + 5 + 6 + 7 = 28。

前10个三角数为:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

先让我们来看看前7个三角数各自都有哪些因数吧:

  • 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

可见。28是第一个拥有超过5个因数的三角数。

那么第一个拥有超过500个因数的三角数是……?

解答:

import time
def getCount(a):
	count=0
	b=a
	if a==1:
		return 1
	if a%2==0:
		i=2
	else :
		i=3
	while i < b:
		if a%i==0:
			count+=2
			b=a/i	
			if b==i:
				count-=1
				break		
		i+=1
	return count+2
s=0
i=1
start=time.time()
while True:
	s += i
	size=getCount(s)
	if size >= 500:
		break
	i+=1;
stop=time.time()
print "time is ",stop-start
print s