码迷,mamicode.com
首页 > 其他好文 > 详细

projecteuler---->problem=12----Highly divisible triangular number

时间:2014-06-01 10:31:26      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:python

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	
	
	


projecteuler---->problem=12----Highly divisible triangular number,布布扣,bubuko.com

projecteuler---->problem=12----Highly divisible triangular number

标签:python

原文地址:http://blog.csdn.net/q745401990/article/details/27810969

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!