sum = 0;
for (i = 1; i <= n; i++) { //#1
for (j = 1; j <= i * i; j++) { //#2
if (j % i == 0) { //#3
for (k = 1; k <= j; k++) { //#4
sum++;
}
}
}
}
The above got me confusing
Suppose #1 runs for N times
#2 runs for N^2 times
#3 runs for N/c since for N inputs N/c could be true conditions
#4 runs for N times
Therefore roughly I could be looking at O(N^5) . I am not sure. Please help clarify.
EDIT I was wondering the runtime at the if(j%i==0)
. Since it takes N^2
inputs from its parent loop it could be doing (N^2)/c
executions instead of N/c
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…