-1

I've learned that a while loop such as

int i = 100;
while (i >= 1){
     ...
     ///Stuff
     i = i/2
}

will run in logarithmic time, specifically, O(logn), since it keeps dividing in half each time (like a binary search).

However, what if my while loop looks like this

 int i = 100;
    while (i >= 1){
         ...
         ///Stuff
         i = i/3
    }

Is the complexity still O(logn)?

Can someone explain yes/no and why?

Raphael
  • 72,336
  • 29
  • 179
  • 389
CodyBugstein
  • 2,957
  • 10
  • 30
  • 45

1 Answers1

3

Hint: The number of times the loop is run, assuming it starts at $n$ and divides by $b$ each time, is exactly $\lfloor \log_b n \rfloor + 1$.

Yuval Filmus
  • 276,994
  • 27
  • 311
  • 503