Highly divisible triangular number

我的那个暴力求解,太耗时间了。

用了网上产的什么因式分解,质因数之类的。确实快!还是数学基础不行,只能知道大约。

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?


from math import sqrt
import time
def natSum(n):
x = 1
count = 0
sum = 0
while count <= n:
sum += x
count = 0
for i in range(1,int(sqrt(sum))+1):
if sum % i == 0:
count += 2
if sqrt(sum)==int(sqrt(sum)):
count -= 1
print x,sum,count,n

x += 1

natSum(500)
‘‘‘
start=time.time()
now=2
num=1
t = 0
while t <= 500 :
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

print num
print num
print time.time()-start
‘‘‘

时间: 2024-10-19 08:10:45

Highly divisible triangular number的相关文章

Project Euler:Problem 12 Highly divisible triangular number

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 fir

projecteuler----&gt;problem=12----Highly divisible triangular number

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

Project-Euler problem 1-50

最近闲的做了下Project Euler 上的题目,前面50题都比较简单,简单总结下.代码一般是Python和C/C++的 用Python 做这些题目简直是酸爽啊 一下代码可能不一定是我的,因为不知道论坛里面的回复不是永久的,所以我的代码有的丢了,可能找个和我的意思相近的代码.题目翻译是从 欧拉计划 | Project Euler 中文翻译站上面Copy 的表告我. Problem 1  Multiples of 3 and 5 10以下的自然数中,属于3和5的倍数的有3,5,6和9,它们之和是

Triangular Sums

描述 The nth Triangular number, T(n) = 1 + … + n, is the sum of the first n integers. It is the number of points in a triangular array with n points on side. For example T(4): XX XX X XX X X X Write a program to compute the weighted sum of triangular n

南阳122(Triangular Sums)

Triangular Sums 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 The nth Triangular number, T(n) = 1 + … + n, is the sum of the first n integers. It is the number of points in a triangular array with n points on side. For example T(4): XX XX X XX X X X Write a

POJ 3086 Triangular Sums

Triangular Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6371   Accepted: 4529 Description The nth Triangular number, T(n) = 1 + - + n, is the sum of the first n integers. It is the number of points in a triangular array with n po

cf 47A

F - Triangular numbers Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 47A Appoint description:  System Crawler  (2015-01-09) Description A triangular number is the number of dots in an

Objective-C 程序设计(第六版)第五章习题答案

1. NSLog(@"数字N N的平方"); NSLog(@"--------------------"); for (int n = 1; n <= 10; n++) { NSLog(@"%2d %d",n,n*n); } //结果如下 2014-09-27 15:00:26.457 prog1[1218:303] 数字N N的平方 2014-09-27 15:00:26.459 prog1[1218:303] -------------

CodeChef SEALCM Sereja and LCM(矩阵快速幂)

Sereja and LCM Problem code: SEALCM Submit All Submissions All submissions for this problem are available. Read problems statements in Mandarin Chinese and Russian. In this problem Sereja is interested in number of arrays A[1], A[2], ..., A[N] (1 ≤ A