This is a possible interview question.
Q: List three methods of checking whether any given integers are the power of 2. i.e.
A: The first one is bruteforce, the complexity is
from math import log, floor
from time import time
def chk1(n):
if n < 1:
return False
if n <= 2: # 2^0 = 1, 2^1 = 2
return True
i = 2
while True:
i *= 2
if i == n:
return True
if i > n:
return False
def chk2(n):
if n < 1:
return False
i = log(n, 2)
# might have precision problem
return abs(i - floor(i)) <= 1e-9
def chk3(n):
if n < 1:
return False
return (n & (n - 1)) == 0
def test(n, x):
for i in xrange(0, int(n)): # launch n tests
x(i)
if __name__ == "__main__":
time1 = time();
test(1e6, chk1)
print "chk1 = %.5f" % (time() - time1)
time1 = time();
test(1e6, chk2)
print "chk2 = %.5f" % (time() - time1)
time1 = time();
test(1e6, chk3)
print "chk3 = %.5f" % (time() - time1)
The performance timings are:
chk1 = 2.92900
chk2 = 0.68100
chk3 = 0.21800
It is obvious that the integer method chk3 is the most efficient among the three.
See also: Teaching Kids Programming – Algorithms of Power of Two and C++ Function to Check if Integer is Power of Two
–EOF (The Ultimate Computing & Technology Blog) —
Last Post: Interview Question: Simple Division
Next Post: Runlength Compression Algorithm, Demonstration in Python