Show problems as
Classic problem #342EasyMathBit ManipulationRecursion

Power of Four

Real-world version: Is the Number a Power of Four
Tracked in this browser only
Read Power of Four on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Return true when the whole number n is a power of four, meaning n equals 4 raised to some whole power of zero or more.

Examples

Example 1

Input
n = 64
Output
true

Four multiplied by itself three times comes to 64.

Example 2

Input
n = 8
Output
false

Eight is a power of two but not of four: dividing it by four leaves two behind.

Example 3

Input
n = 0
Output
false

Every power of four is at least one, so nothing this small qualifies.

Constraints

  • -2^31 <= n <= 2^31 - 1

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def is_power_of_four(n: int) -> bool:
Java
public boolean isPowerOfFour(int n)