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

project euler 12 Highly divisible triangular number的更多相关文章

  1. Project Euler Problem 12-Highly divisible triangular number

    最直接的想法就是暴力搞搞,直接枚举,暴力分解因子.再好一点,就打个素数表来分解因子.假设num=p1^a1p2^a2...pn^an,则所有因子个数为(a1+1)(a2+1)...(an+1). 再好 ...

  2. Highly divisible triangular number

    我的那个暴力求解,太耗时间了. 用了网上产的什么因式分解,质因数之类的.确实快!还是数学基础不行,只能知道大约. The sequence of triangle numbers is generat ...

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

    title: The sequence of triangle numbers is generated by adding the natural numbers. So the 7th trian ...

  4. Python练习题 040:Project Euler 012:有超过500个因子的三角形数

    本题来自 Project Euler 第12题:https://projecteuler.net/problem=12 # Project Euler: Problem 12: Highly divi ...

  5. Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.

    In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentago ...

  6. Python练习题 048:Project Euler 021:10000以内所有亲和数之和

    本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable ...

  7. Python练习题 039:Project Euler 011:网格中4个数字的最大乘积

    本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...

  8. Python练习题 033:Project Euler 005:最小公倍数

    本题来自 Project Euler 第5题:https://projecteuler.net/problem=5 # Project Euler: Problem 5: Smallest multi ...

  9. [project euler] program 4

    上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...

随机推荐

  1. python学习第一天 -安装配置及其输入输出

    Python, 是一种面向对象.解释型计算机程序设计语言. python适合领域: 1.Web网络和各种网络服务 2.系统工具和脚本 3.作为“胶水”语言把其他语言开发的模块包装起来方便使用 pyth ...

  2. juqery easy ui 实现二级菜单联动

    实现效果 代码: <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat= ...

  3. R教程计划

    提起数据挖掘,似乎会有很多人望而却步,从生产规划到到规律分析,从生物医学到航天科技,到处都有数据挖掘工程师留下的影子. 通过对比SAS,SPSS,以及R,最终选定了R, 不为什么,免费且高效才是硬道理 ...

  4. Discuz!提取文章标签

    <?php    //强制使用字符集 @header('Content-Type: text/html; charset=gbk');    $subjectenc ='title'; //这是 ...

  5. deflate与gzip

    gzip是一种数据格式,默认且目前仅使用deflate算法压缩data部分: deflate是一种压缩算法,是huffman编码的一种加强. deflate与gzip解压的代码几乎相同,可以合成一块代 ...

  6. hdu1561-The more, The Better(树形dp)

    Problem Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物.但由于地理位置原因,有 ...

  7. Python多线程(threading模块)

    线程(thread)是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位.一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务. ...

  8. Unity四种路径总结

    四种路径的权限:                                            Application.dataPath 包含游戏数据文件夹的路径(只读) Applicatio ...

  9. deciaml(十进制浮点运算)

    # -*- coding: utf-8 -*- # ==================== #File: python #Author: python #Date: 2014 #========== ...

  10. Super Jumping! Jumping! Jumping!(dp)

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...