for(i = 1; i < n*n; i++){
for(j = 1; j < i*i; j++){
if(j % i == 0){
for(k=0; k < j; k++){
count++;
}
}
}
}
My attempt at a solution:
j iterates up to i*i = n^4. For the ‘k’ loop, we have the sum of k from 1 to n^4 which is n^4(n^4-1)/2. So the runtime is O(n^8). This strikes me as too high, but I don’t see an error.
The outer loop executes n2 times. The next loop executes a total of ∑i=1n2 i2, which is O(n6).
The innermost loop only runs when
jis a multiple ofi, which happensitimes for each value ofi. The innermost loop executesjtimes for each such value ofj:i,2i,3i, and so forth untili*i. Thus, the innermost loop executes ∑j=1i ij times, which is O(i3), for eachi.Therefore, the total running time is ∑i=1n2 O(i3) + O(n6), which is O(n8) since ∑i=1n2 O(i3) = O(n8).
(Note that I’m assuming the second loop increments
j++, noti++. The answer is rather different if it’si++).