The answer $4n$ is correct if we count arithmetic operations and don't count comparisons.
I assume that multiplications and additions are counted as basic operations with complexity $1$. This is an abstraction and is not indicative of how fast simple loops like this would be executed on a modern hardware after compilation using a modern compiler.
To execute the statement partialSum += i * i * i
we need to perform $3$ basic operations:
- multiply
i
by i
- multiply the result of the previous by
i
- add the result of the previous to
partialSum
Another operations that needs to happen at every iteration, which you have omitted, are the increment/decrement of the loop counter and the check for the end of the loop. So I only get $4$ operations per iteration if I count the increment and not count the comparison, or assume that they are done simultaneously. In total this gives $4n$, where $n$ is the number of iterations.