Power of Two
MATH
Problem
Given an integer n
, this function checks whether it is a power of two.
A number n is a power of two if there exists an integer x such that n=2^x
.
The function returns true
if n
is a power of two and false
otherwise.
The solution involves efficient mathematical checks rather than iterating through all possible powers.
Examples
isPowerOfTwo(2) // true // Why? 2^1 = 2 isPowerOfTwo(4) // true // Why? 2^2 = 4 isPowerOfTwo(5) // false
Loading...