I am analyzing an algorithm and I just want to know if I am on the right track.
For this algorithm, I am only counting the multiplications on the line which have *** in them.
Here’s the algorithm:

- So I am starting from the inner most line, I can see there are 2 operations there (the two multiplications).
- Now I am looking at the 2 inner most loops, so I can tell that the
p=p*20*zgets executed exactly(j) + (j-1)+(j-2)+(j-3)...1times. This happens to be equal toj(j+1)/2. - So in total, since there are two multiplication, it happens
2 * (j(j+1)/2). - Then finally, the “i” loop happens exactly n times, so, in total, it’s
n(2 * (n(n+1)/2)).
That’s my thought process behind this. Am I correct? Thanks.
Your thought process is correct. You need to replace the j term with an n (n being the largest value j can assume), but that is probably a typo.
Furthermore, you can simplify further from where you are:
The last step is because the n cubed term will grow at a much larger rate than the n squared term we can say it will dominate the runtime for large n. Only other point I would mention is that you should perhaps consider the store to p as an operation as well as the two multiplication, although obviously this will not change the simplified big-o runtime.