int x = 0;
for (int i = n; i >= 3; i--) {
for (int j = 1; j <= Math.log(i) / Math.log(2); j++) {
for (int t = 0; t <= n; t += j) {
x++;
}
}
}
System.out.println(x);
As you can see I have 3 for loops whose conditions depend on each other.
My analysis:
- The first loop: I assumed that it will run
(n-2)
times "worst case" scenario. - The second loop: I assumed it will run
log(n)
times "worst case" scenario. - The third loop: I assumed it will run
(n)
times "worst case" scenario.
So I have calculated that the function of the 3 loops will be:
(n-2)*(log(n))*(n)=(n^2)*log(n)-(2n)*log(n) = O((n^2)*log(n))
I'm not sure that my calculation is correct, please do advise!