这个勾股三元组函数的复杂性是多少


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(

这个问题可以提供进一步的启示,嵌套循环的时间复杂性

最新更新