As part of an event, I had to come up with a number of problems to be solved in Python. One was to write a procedure to determine if a number is a power of two. I supplied answers for the judges and for this problem I provided:
while x > 1 and (x % 2 == 0):
x = x//2
return x == 1
return not (math.log(x, 2) % 1)
But one of the participants came up with a solution that I hadn't thought of and for some reason appeals to me the most out of the three. This is my version:
s = bin(x)[2:]
return s == "1" and s[1:] == "0"*len(s[1:])
bin is built into the language, but I've never used it and had actually forgotten that it existed.
Let me know if you have other ways of solving this that do not involve pre-computing large sets of data in advance.