How do you calculate the amount of iterations the loop "for (i = 1; i < n^3; i += n)"

How do you calculate the amount of iterations the following loop will have in terms of n?:

for (i = 1; i < n^3; i += n) {
}


I've gotten as far as: $$i=(x-1)n+1\ for\ current\ iteration=x$$ $$Total\ iterations=x\ when\ n(x-1)+1\geq n^3>n(x-2)+1$$

• n^3 is an exclusive or, changing the lowest two bits in n. Mar 12 at 16:17
• @gnasher729: not in Matlab, Scilab, R, Basic nor Excel. :)
– user16034
Mar 12 at 16:53
• @gnasher729 Yeah thanks I know. The idea behind the question is the math not whether the code works. I'm just trying to calculate the amount of iterations it would have. Mar 13 at 11:36

1 Answer

Hint:

$$i=1,1+n,1+2n,1+3n,\cdots1+(n^2-1)n,1+n^2n\cdots$$

• During final iteration x, i = 1 + (n^2 - 1)n; Total iterations = x when 1 + (n^2 - 1)n = 1 + (x - 1) = n^2; Thank you. I simply couldn't see it. Mar 13 at 13:19
• @user19843013: your development is approximate, to say the least.
– user16034
Mar 13 at 14:04
• Whatever that means Mar 14 at 6:48