Math Fundamentals18 sections · 814 units
Open in Course

Power of Two - Algorithm

Step by step approach

Here's the approach:

1.1. If n0n \leq 0, return false (powers of two are positive)

2.2. While n>1n > 1, check if nmod2=0n \bmod 2 = 0

3.3. If remainder is not 00, return false

4.4. Otherwise, divide nn by 22 and continue

5.5. If the loop completes (n=1n = 1), return true