题目
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:

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?
解答
这道题首先是生成所谓的三角数序列:
def tri():
res = 1
i = 2
while True:
yield res
res += i
i += 1
然后我们需要找因子,再计个数嘛。
def get_factors(N):
d = np.arange(1, np.sqrt(N)+1)
res = d[N % d == 0]
res = np.append(res, N/res[::-1])
return np.unique(res)
def nfac(N):
factors = get_factors(N)
return factors.size
然后就是迭代,数数目,符合条件则返回结果:
def solution12(n):
for i in tri():
res = nfac(i)
if res > n:
break
return i
这道题解出来是1.5s。
