[英]How to make my program more efficient in Python
So I had to make a code which generates the first triangle number which has over 500 factors. 所以我不得不制作一个生成第一个三角形数的代码,该数字超过500个因子。 The problem is given in detail below:
问题详述如下:
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:
所以第7个三角形数字是1 + 2 + 3 + 4 + 5 + 6 + 7 = 28.前十个术语是:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... 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. 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我们可以看到28是第一个超过五个除数的三角形数。
What is the value of the first triangle number to have over five hundred divisors? 拥有超过500个除数的第一个三角形数的值是多少?
I have written a block of code which generates the same however it is highly inefficient ; 我编写了一段生成相同代码的代码,但效率非常低 ; kindly suggest some ways to improve it.
请提出一些改进方法。 Moreover it is so inefficient that it only works with numbers below 70
此外它效率很低,只适用于70以下的数字
My code is given below, please refer: 我的代码如下,请参考:
def generates_triangle_numbers_upto_n(n):
list = [1]
while len(list)<n:
nth = int(len(list)+1)
to_be_appended = nth/2 + nth**2/2
list.append(to_be_appended)
return list
def return_number_of_n(n):
num = 0
for i in range(2, int(n)):
if n%i == 0:
num = num+1
return num + 2
def main(n):
list = generates_triangle_numbers_upto_n(20000)
for i in list:
if return_number_of_n(i) > int(n):
return i
print(main(100))
I saw a similar question on this site but I didn't understand how it worked: 我在这个网站上看到了类似的问题,但我不明白它是如何工作的:
Thanks a lot! 非常感谢!
Edit 1: Thanks everyone for the wonderful suggestions, based on which I have refined my code: 编辑1: 感谢大家提出的精彩建议,我根据这些建议改进了我的代码:
def main(n):
list = [1]
while return_number_of_n_second(list[len(list)-1]) <= n:
nth = int(len(list)+1)
to_be_appended = int(nth/2 + nth**2/2)
list.append(to_be_appended)
return list[len(list)-1]
def return_number_of_n_second(n):
num = 0
import math
sqrt = math.sqrt(n)
for i in range(2, math.ceil(math.sqrt(n))):
if n%i == 0:
num = num+1
if int(sqrt) == sqrt:
return num*2 +3
return num*2 + 2
print(main(500))
However, now too, it takes 10-15 seconds to execute. 但是,现在也需要10-15秒才能执行。 Is there a way to make it even more efficient since almost all of project euler's problems are to be executed in 2-3 seconds max?
有没有办法让它更有效率,因为几乎所有的项目欧拉的问题都要在2-3秒内完成?
Just some basic technical optimizations and it should do it: 只是一些基本的技术优化,它应该这样做:
import time
import math
def main(n):
last, length = 1, 1
while return_number_of_n_second(last) <= n:
length += 1
last = int(length/2 * (length+1))
return last
def return_number_of_n_second(n):
sqrt = math.sqrt(n)
if int(sqrt) == sqrt:
return 2
return sum(1 for i in range(2, math.ceil(sqrt)) if not n % i) * 2 + 2
start_time = time.time()
print(main(500))
print(time.time() - start_time)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.