if x:
for i in range(a):
for z in range(a):
for k in range(z):
for p in range(i):
c = (i * z) + (k * p)
else:
for i in range(a):
for z in range(a):
for k in range(z):
c = (i * z) + (k * p)
Would this be O(n^4)? Also, how many multiplications would occur?
EDIT: updated the code. Also, since the lower bound captures the max number of steps a valid input will force, wouldn’t big omega be n^4 as well?
Yes, the complexity is still
O(n^4). To make things simple, here is the trick to rearrange your codewhere
f(i, p)isIn the first part,
f(i, p)has been executed forO(n^2/2)up to the largest order (because of the summationsum_i (i^2), do the math yourself). Similarly, thef(i, p)has the complexity off(i, p)which is again equal toO(n^2/2).So the combined resulting order is
O(n^4/4). and there is two multiplications for each operation, so number of multiplication isO(n^4/2)