-1
def function(N):
    foo = 1
    while foo < N:
         foo *= 2
    return foo

I think it's O(log N), since foo is being multiplied by a constant amount?

Pig
  • 2,002
  • 5
  • 26
  • 42

1 Answers1

1

This is something you can easily show by the definition of logarithm. For simplicity we consider base 2 logarithm (all log bases are equivalent in big-O notation since they are all within a constant factor of each other).

log2 n = k

means:

2 ** k = n

but 2 ** k is actually 2 * 2 * 2 ... * 2 (k times) -- for integer k. For any given n, k may not be integer, but you can safely consider the next positive integer k as that guarantees that 2 ** k is larger than n.

So, the number of * 2 operation needed to go from 1 to n is k (within unity).

norok2
  • 25,683
  • 4
  • 73
  • 99