I have this rather complicated loop:
sum=0
for i=1 to n do
for j=1 to i^2 do
if(j (mod i) = 0) then
for k=1 to j do
sum++
How the heck do I analyze this? I would try to use a sum but the if statement throws me off. How can I determine what time this nested for loop runs in?
The code is essentially this:
The $ k $ loop executes $ j^2 $ times. The $ j $ loop executes $ i $ times
Therefore the time taken to execute all the iterations of a $ j $ loop is
$ \sum\limits_{j = 1}^{i}{j^2} = O(i^3) $
Finally, the $ i $ loop executes $ n $ times, the time taken to execute the whole program will be
$ \sum\limits_{i = 1}^{n}{O(i^3)} = O(n^4) $.
That's all.