342 Power of Four

Problem Statement

Given an integer n, return true if it is a power of four. Otherwise, return false.
An integer n is a power of four, if there exists an integer x such that n == 4x.

Input: n = 16
Output: true

Input: n = 5
Output: false

Input: n = 1
Output: true

Approach

We can solve this with Recursion where we recursively call the function with n = n / 4.
The base case has to be defined as n === 1. If we reach 1 we know that the passed in number is a power of four.
To get to the base case we have to understand exponents. If the integer n is equal to 1 it can be expressed as 4^0, which satisfies the definition of a power of four and is the smallest possible case of power of four.

Solution

var isPowerOfFour = function(n) {
  if (n === 1)  return true
  if (n < 1) return false
  return isPowerOfFour(n / 4)
};

Questions

References

LC

Recursion