1
$\begingroup$

So, I'm trying to understand how to get the run time of this loop:

for(int i = 1; i < n*n*n; i*=3) {...}

So, far I know:

  • loop starts at 1
  • finishes when $i > n^3-1$
  • that $i$ is multiplied by 3 each iteration.

Intuitively it seems like as $n$ gets larger the number of iterations decreases which makes me think that the run-time is $O(log$ $n)$. My question is if this is correct if it is what is a mathematical way I could derive that answer?

$\endgroup$

1 Answer 1

1
$\begingroup$

That's right. This for loop stops when: $$ 3^i < n^3 \rightarrow i<3\log(n) $$ Which implies the complexity of this for loop is $O(\log(n))$.

$\endgroup$
1
  • 1
    $\begingroup$ Could you explain a little more on how you reached that conclusion? $\endgroup$
    – KetDog
    Sep 17, 2019 at 23:43

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.