2013-11-27 35 views
-1

我的代码表明波纹管发现任何两个数字之间的三角数

import math,sys 

#create a list with numbers 
def create_list(): 
    num_list=[] 
    for num in range(int(input("insert start point: ")),int(input("Insert end point: "))): 
     num_list.append(num) 
    return num_list 

#function to find triangular numbers 
def get_triangles(numlist): 
    triangles = [] 
    for i in numlist: 
     if (check_triangle(i)): 
      triangles.append(i) 
    return triangles 

#function to check number is triangular or not 
def check_triangle(n): 
    return math.sqrt((8*n)+1).is_integer() 

#function main to run the process 
def main(): 
    numlist = create_list() 
    print(get_triangles(numlist)) 

即使它看起来像任务完成它不是。我尝试了范围为0 - 100000000(1 * 10^8)的数字。这是因为我的笔记本电脑卡住了任何可以完成此任务的方法?

+0

您的程序正常运行,当您尝试查找如此大的数字时,效率不高。你的问题是什么?为什么你需要检查这么大的范围?不是逐个检查每个数字,而只是生成三角数字的序列,直到找到大于10^8的数字为止? – hankd

+1

您正在建立一个包含1亿个数字的列表,然后无条件地将其复制到第二个列表(并以尽可能最慢的方式),然后*执行其他1亿次。这是......需要一些时间。 – geoffspear

+0

我对你的算法的质量没有任何要求 - 确实有比检查范围内的每个整数更好的方法 - 但更好的实现你选择的算法的方法是'triangles = itertools.ifilter(check_triangle,xrange(start,end) )'。这将返回一个将产生三角形数字的迭代器;如果你确实需要列表,你可以调用'list(triangles)'。 –

回答

0

看来你只是试图在一个范围内生成所有的三角形数字。如果是这样,直接计算它们比通过平方根检查快得多。

请注意,您可以通过简单地添加连续数字来生成三角形数字。

T_0 = 0 
T_1 = T_0 + 1 = 1 
T_2 = T_1 + 2 = 3 
T_3 = T_2 + 3 = 6 
... 

如果你想保持它的简单,你可以创建一个功能,以保持从n = 0产生这些数字,让他们当他们进入期望的范围,并继续下去,直到它超过了上限。

def generate_triangular_numbers(a, b): 
    """Generates the triangular numbers in [a, b] (inclusive)""" 
    n, t = 0, 0 
    triangles = [] 

    while t < a: 
     n += 1 
     t += n 

    while t <= b: 
     triangles.append(t) 
     n += 1 
     t += n 

    return triangles 
+0

嗨,感谢您的评论,但问题是我可以使用的最大范围。例如min = 0和max = 10000000000000那么它不工作或产生错误的结果.. –

2

不要打印一个大的表格。而是把它写入文件,这样你可以在后面打开文件。该程序无法高效地将更多的信息写入控制台。我发现在控制台上打印内容会使程序效率降低很多。

此外,我读了一些关于您的代码的评论,他们声明效率不高,我不得不同意。

这是我写的一段代码。这需要一点解释,但我很匆忙。只需回复,如果你需要帮助理解它。

def getTriangles(input1,input2): #input1 is the min value and input2 is the max value 
    li = [] #the list where all of the numbers will go 
    i = 0 #an integer that acts how much another layer of the triangle would have 
    j = 0 #the most current number that it is on 
    while True: #I whipped up this algorithm in a couple minutes, so there is probably a more efficient way than just looping through all of them, but it is faster than the current one being used 
     i += 1 #i has to increment to act as the increase of a side 
     if j > input2: #if the value that could be added is greater than the max number, than just end the function and return the list 
      return li 
     if j >= input1: #if the number qualifies the minimum number requirements, then the program will add it to the list, otherwise it will ignore it and continue on with the function 
      li.append(j) 
     j += i #this simulates adding in another layer of the triangle to the bottom 

这将是一个方式来使用它: 打印(getTriangles(1,45)) 我相信你可以看一下如何将内容写入一个文件。

+0

非常感谢你,我得到的代码。我试着用线程播种算法,就像下面你对此有什么想法一样。我是蟒蛇的新来者。所以非常感谢你的帮助 –

+0

“NAME:斩下来算法 任务:增加搜索任务的效率变化大域 算法: 假设域的大小等于M,其中M> 10E6 开始 砍掉域到n部分,其中n <= M/[(M * 5)/ 100] 创建n个单独线程(避免资源查杀) 向每个线程分配单个子域 执行搜索 结束' –

+0

效率最高实现这一点的方法是通过一个函数,它可以告诉查找三角形数字的函数大致相等,然后连续执行,直到找到第一个值。既然你现在有了“n”,你可以在几个线程中继续使用我的函数的修改版本。 – user3033423

相关问题