The Big-O of the function is not constant time.
The Big-O of the function will be the same as the Big-O of the function math.log
. This basically depends on the implementation of the function. (The math.floor
function can be implemented in constant time).
The log
function is typically computed using the Taylor-series expansion and is O(M(n) * n^0.5)
where M(n)
is the complexity of multiplying two n-digit numbers.
For more information on this, please look at this link.
Note: If you want to check if a number is a power of 2
, all you need to do is the following check with binary arithmetic
def pow_of_2(x):
return ((x & (x - 1)) == 0)
Basically you need to check if exactly one bit is set to 1
in the binary representation. A more detailed explanation of how this works is here.