Highly divisible triangular number

Problem 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?


def divisor(num,j):
count1 = 1
count2 = 1
if num%2 == 0:
lst1 = [num//2]
lst2 = [num+1]
else:
lst1 = [(num+1)//2]
lst2 = [num]
for i in range(2,lst1[0]):
if lst1[0] % i ==0:
count1 += 1
# print(i)
lst1.append(i)
for i in range(2,lst2[0]):
if lst2[0] % i ==0:
count2 += 1
lst2.append(i)
count = count1 + count2 + count1*count2
return count n = 1
import time
print(time.time())
while True:
j = n*(n+1)//2
if str(j)[-1] != '':
#print(n)
n += 1
continue
count = divisor(n,j)
# print(j,n,count)
n += 1
if count >= 500:
print(j,n,count)
break print(time.time()) 1462342495.953848
76576500 12376 575
1462342499.651143

约4S

对求因子的算法进行一点改进,能提高一半效率。

    for i in range(2,lst1[0]//2+1):

    for i in range(2,lst2[0]//2+1):

>>>
1462414510.288464
76576500 12376 575
1462414512.207313

再次对求因子进行改进,效率提高10倍!!

    for i in range(2,int(sqrt(lst1[0])+1)):
if lst1[0] % i ==0:
count1 += 2
1462415389.778016
76576500 12376 575
1462415389.91842
>>>

最新文章

  1. PHP浮点数精度问题
  2. JS验证控件jQuery Validate
  3. iOS不用调用,running time自动执行方法
  4. MySQL 临时表的使用
  5. 那些教程没有的php1-基础知识补漏
  6. chrome 浏览器 开发者工具 性能检测 参数解释
  7. DDL、DML和DCL的理解
  8. 【HDOJ】1474 Always On the Run
  9. 下载doxygen
  10. JS(一)
  11. MySQL percona-toolkit工具包的使用教程
  12. NYNU_省赛选拔题(5)
  13. 最好用的MongoDB GUI - LivingMongo
  14. C语言第十次作业
  15. [物理学与PDEs]第1章第3节 真空中的 Maxwell 方程组, Lorentz 力 3.2 Lorentz 力
  16. Selenium自动化获取Http报文信息并判断当前API状态
  17. border三角形
  18. DataBrewery Cubes 连接Kylin
  19. 【AtCoder】ARC085
  20. C#之asp.net 及MVC 生成动态验证码:

热门文章

  1. 100个iOS开发/设计程序员面试题汇总,你将如何作答?
  2. Core Data入门-备用
  3. linux安装xunsearch
  4. ACdream 1017 Fast Transportation
  5. gridview两列数据的互换
  6. Ghost源代码
  7. 【POJ3006】Dirichlet's Theorem on Arithmetic Progressions(素数筛法)
  8. The Hitchhiker’s Guide to Python! — The Hitchhiker's Guide to Python
  9. python学习之路-4 内置函数和装饰器
  10. C++ - 容器(container)的erase()函数