Analysis of the running time (Big Oh notation)

I have a nested for loop and I am supposed to calculate the running time (in Big-Oh notation) of the nested for loop. That is, whether it is linear (O(N)), quadratic (O(N^2)), cubic (O(N)^3) and so on.
sum = 0;
for (i = 1; i < n; i++)
{
for(j = 1; j < i*i ;j++)
if(j%i == 0)
for(k = 0; k < j; k++)
sum++;

So the statement inside the innermost for-loop doesn't account for much so we can safely ignore it. My book says the running time would be equal to the product of the sizes of all for loops. Size == number of iterations. Hence, the size of the outermost loop = n. Does that imply the size of the middle loop is n^2? Probably not. The middle loop does way more iterations that n^2. Furthermore, since Big Oh is just an upperbound, some scribbling led me to O(N^3) for middle loop. Likewise, I got O(N^4) for the innermost.
This question took an entire day, and I am not willing to spend anymore time with it. Your input would be much appreciated.
Last edited on
Sounds about right. The middle loop is O(n³) and in addition to that, there's c*n³/n*n² iterations of the inner loop, resulting in O(n^4).
Topic archived. No new replies allowed.