gpt4 book ai didi

python - 这个毕达哥拉斯三重函数的复杂度是多少?

转载 作者:行者123 更新时间:2023-12-01 01:28:21 25 4
gpt4 key购买 nike

def tuplePyth(n):
list_=[]
for x in range(1, n):
for y in range(x + 1, (n - x) // 2):
for z in range (y + 1, n - x - y):
if smallestTrip(x, y, z)==False:
list_.append([x,y,z])
print (list_)

def pythTrue(a,b,c):
(A,B,C) = (a*a,b*b,c*c)
if A + B == C or B + C == A or A + C == B:
return True

def smallestTrip(a,b,c):
if pythTrue(a,b,c) == True:
if (a+b+c)%12 == 0:
return True
else:
return False

smallestTrip 检查 x,y,z 是否是基本 3,4,5 直角三角形的倍数。

目标是生成所有可能的勾股三元组,其总和小于输入的总和 n。

(这些三元组不能是 (3,4,5) 三角形的倍数。)

这里的复杂度是O(nnlogn)吗?

最佳答案

其他函数的复杂度为 O(1),并且对于原始问题中的 n 存在三个循环。所以复杂度是 O(n * n * n) = O(n^3)

这个问题可能会提供进一步的启发Time complexity of nested for-loop

关于python - 这个毕达哥拉斯三重函数的复杂度是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53141704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com