Given the following code fragment:
int sum = 0;
for (int i = 1; i <= N; i++)
for (int j = 1; j <= i*i; j++)
for (int k = 1; k <= j*j; k++)
sum++;
My assumption:
Hence, total running time should be O(N^5), right?
A preliminary remark
sum(k=1,p,k^2) = p(p+1)(2p+1)/6 = O(p^3)
sum(k=1,p,k^6) = O(p^7)
Computation of complexity
k=1
to j^2
so it does exactly j^2
operations.j=1
to i^2
and at each step we do j^2
operations. According to my preliminary observation, by substituting p=i^2
in the first equation, we can compute the total operations as: i^2(i^2+1)(2*i^2+1)/6
for each value of i
. This is an O((i^2)^3) = O(i^6)
number of operations.i=1
to n
and does O(i^6)
operations at each step. So we have O(n^7)
operations.References
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With