0
$\begingroup$

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$$

$\endgroup$
3
  • $\begingroup$ n^3 is an exclusive or, changing the lowest two bits in n. $\endgroup$
    – gnasher729
    Commented Mar 12, 2023 at 16:17
  • 2
    $\begingroup$ @gnasher729: not in Matlab, Scilab, R, Basic nor Excel. :) $\endgroup$
    – user16034
    Commented Mar 12, 2023 at 16:53
  • $\begingroup$ @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. $\endgroup$ Commented Mar 13, 2023 at 11:36

1 Answer 1

0
$\begingroup$

Hint:

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

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$ Commented Mar 13, 2023 at 13:19
  • $\begingroup$ @user19843013: your development is approximate, to say the least. $\endgroup$
    – user16034
    Commented Mar 13, 2023 at 14:04
  • $\begingroup$ Whatever that means $\endgroup$ Commented Mar 14, 2023 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.