project euler 12 Highly divisible triangular number
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的更多相关文章
- Project Euler Problem 12-Highly divisible triangular number
最直接的想法就是暴力搞搞,直接枚举,暴力分解因子.再好一点,就打个素数表来分解因子.假设num=p1^a1p2^a2...pn^an,则所有因子个数为(a1+1)(a2+1)...(an+1). 再好 ...
- Highly divisible triangular number
我的那个暴力求解,太耗时间了. 用了网上产的什么因式分解,质因数之类的.确实快!还是数学基础不行,只能知道大约. The sequence of triangle numbers is generat ...
- projecteuler---->problem=12----Highly divisible triangular number
title: The sequence of triangle numbers is generated by adding the natural numbers. So the 7th trian ...
- Python练习题 040:Project Euler 012:有超过500个因子的三角形数
本题来自 Project Euler 第12题:https://projecteuler.net/problem=12 # Project Euler: Problem 12: Highly divi ...
- 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 ...
- Python练习题 048:Project Euler 021:10000以内所有亲和数之和
本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable ...
- Python练习题 039:Project Euler 011:网格中4个数字的最大乘积
本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...
- Python练习题 033:Project Euler 005:最小公倍数
本题来自 Project Euler 第5题:https://projecteuler.net/problem=5 # Project Euler: Problem 5: Smallest multi ...
- [project euler] program 4
上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...
随机推荐
- python - num1 -初识python
一.了解python python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC ...
- hdfs的实现机制和文件系统概念
1.HDFS的诞生背景: 数据量太大,在一个结点(机器)存不下.所以需要分布式存储,HDFS就是hadoop的分布式文件系统,来存储分布式数据. 2.共享文件系统也是一种分布式存储但有缺点:1.并发差 ...
- DataTables获取表单输入框数据
$(document).ready(function() { var table = $('#example').DataTable(); $('button').click(function() { ...
- html5画饼形图
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm10.aspx ...
- 开心菜鸟学习系列-----javascript(2)
最小全局变量 : 1)每个javascript环境有一个全局对象,当你在任意的函数外面使用this的时候可以访问到,你创建的每一个全部变量都成了这个全局对象的属性,在浏览器中,方便起见, ...
- JS 浮点计算BUG
最近做项目的时候遇到一个比较纠结的js浮点计算问题. 当时是做利率计算,因为利率大多数涉及到小数点,精度要求也很高. 0.6+0.1+0.1=? 结果出现:0.7999999999999 网上查找了一 ...
- 全局函数的Result一定要每次都初始化,否则上次的结果会被保持到下一次继续使用
测试半天,原来是因为这个原因.下面例子中,Result:=''必须写,否则其结果会被累计,真是昏倒!! function MyPaths(tache: IXMLTaskType) : String; ...
- Linux配置完iptables后,重启失效的解决方案
Linux配置完iptables后,重启失效的解决方案 因为只有root用户才可访问1024以下的端口,非root用户登陆是不能启用80端口的.web service 往往启动1024以上的端口,并通 ...
- 10.1.5 Connection Character Sets and Collations
10.1.5 Connection Character Sets and Collations Several character set and collation system variables ...
- 一道有关球赛队员分配的C++程序题目
题目描述: 两个球队进行比赛,各出三人.甲队为a,b,c三人,乙队为x,y,z三人.已经抽签决定比赛名单. 有人向队员打听比赛安排的名单.a说他不和x比,c说他不和x,z比,请编程找出三队赛手的名单. ...