No problem. Now, even though doing what I did is very simple and lends the correct result, these are still just words. A lot of times you will be required to show all of this stuff mathematically, in which case you would just have to get down and dirty with series and inequalities
Edit: I hope I'm not beating a dead horse here, but let me suggest an easy mathematical way of solving this thing.First of all, I see that you have already noticed the limits of your last loop - i, 2i, 3i, 4i, ...i^2, because these are the i numbers from 1 to i^2 that are divisible by i itself. Now, here's my suggestion - rewrite your code so that it performs the same number of calculations, but without the annoying if-statement. Here is how the code can be rewritten:
CODE
for( int i =1; i <= n; i++ )
for( int j =1; j <= i; j++ )
for( int k=1; k <= i*j; k++ )
sum++;
Two things to note:
1. The 2nd loop now goes until i. And that's fine, because that's how many times it actually operates.
2. Now the k goes up to i*j, and that makes sense. The i is fixed, and j iterates from 1 to i, giving us the same pattern.
Now this is a lot easier to calculate Big-O of.
This post has been edited by Neumann: 14 Sep, 2009 - 04:46 PM