Classic problem #342EasyMathBit ManipulationRecursion
Power of Four
Real-world version: Is the Number a Power of FourTracked 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)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's