views:

153

answers:

3
while (n >= 1)

n /= 2;

I am unable to get the Big-O notation for this

+1  A: 

Any algorithm which cuts the problem in half each time is O(log(n)).

Shynthriir
Generally when questions are tagged homework the idea is to give them hints to solve the question, and not outright give them the answer. This lets them understand the problem, which is the point of homework.
Paul
would be awesome if you also give an explanation.
zengr
If a teacher actually assigns a problem full credit having just the answer, I think something is wrong. Sometimes a problem is better understood when looking at the answer and working backwards, and then later problems become more understandable.
Shynthriir
you should lead a student to a solution not give them the answer directly.
zengr
+5  A: 

I'll just follow Pointy's advice for the sake of exposition.

Try 8.

4 2 1 0: 4 iterations.

Try 32.

16 8 4 2 1 0: 6 iterations.

Try 66.

33 16 8 4 2 1 0: 7 iterations.

So… how are the initial numbers changing, and how are the numbers of iterations changing?

Potatoswatter
Shouldn't 66 be `33 16 8 4 2 1 0`?
Paul
@Paul: fixed :P
Potatoswatter
@Potatoswatter: Close enough I suppose. :P
Paul
@Paul ROFL, ok now…
Potatoswatter
A: 

T(n) = O(log2n)

steven