Problem 8

Power of Two Recursion Challenge

Given a non-negative integer n, determine whether it is a power of two. A power of two is any number that can be expressed as 2k where k is a non-negative integer (e.g. 1, 2, 4, 8, 16, ...).

Function Signature

powerOfTwo(n)

Parameters

  • n — a non-negative integer.

Output

Return true if n is a power of two, or false otherwise.

Constraints

  • The function must use recursion.
  • The function should accept exactly one argument.

Examples

powerOfTwo(1)   → true
powerOfTwo(2)   → true
powerOfTwo(16)  → true
powerOfTwo(128) → true
powerOfTwo(0)   → false
powerOfTwo(10)  → false
powerOfTwo(270) → false

Edge Cases

  • 0 is not a power of two.
  • 1 is a power of two (20 = 1).