最大分割三角数---Python

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?

求第一个有超过500个除数的三角数。

观察:每个三角数除以前半部分的除数,等于后半部分的除数。

from math import sqrt
import time
start=time.time()
now=2
num=1
while True:
    num=num+now
    now+=1
    t=0
    for x in range(1,int(sqrt(num))+1):
        if num%x==0:
            t+=2
    if sqrt(num)==int(sqrt(num)):
        t=t-1
    if t>500:
        break
     
print num
posted @ 2015-02-05 18:42  liuwenjie  阅读(266)  评论(0编辑  收藏  举报