Here's the complete solution:
function isPowerOfTwo(n)
if n ≤ 0 then
return false
while n > 1
if n mod 2 ≠ 0 then
return false
n := n / 2
return true
Each division by reduces . If at any point is odd (modulo ), you know it's not a power of two. The loop runs at most times.