Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python - Given an integer 'n', check if it is a power of 4, and return True, otherwise False.
When it is required to check if a given variable is a power of 4, we can use different approaches. A power of 4 means the number can be expressed as 4k where k is a non-negative integer (1, 4, 16, 64, 256, etc.).
Method 1: Using Division and Modulus
This method repeatedly divides the number by 4 and checks if there's any remainder ?
def check_power_of_4(num):
if num == 0:
return False
while num != 1:
if num % 4 != 0:
return False
num = num // 4
return True
# Test the function
test_num = 64
print("The number to be checked is:", test_num)
if check_power_of_4(test_num):
print(test_num, "is a power of 4")
else:
print(test_num, "is not a power of 4")
The number to be checked is: 64 64 is a power of 4
Method 2: Using Logarithms
We can use logarithms to check if log4(n) is an integer ?
import math
def is_power_of_4_log(num):
if num <= 0:
return False
log_result = math.log(num, 4)
return log_result == int(log_result)
# Test with multiple numbers
test_numbers = [1, 4, 16, 64, 100, 256]
for num in test_numbers:
result = is_power_of_4_log(num)
print(f"{num} is {'a' if result else 'not a'} power of 4")
1 is a power of 4 4 is a power of 4 16 is a power of 4 64 is a power of 4 100 is not a power of 4 256 is a power of 4
Method 3: Using Bit Manipulation
For powers of 4, we can use bitwise operations. A number is a power of 4 if it's a power of 2 and has bits set only at even positions ?
def is_power_of_4_bitwise(num):
if num <= 0:
return False
# Check if it's a power of 2 and has bits at even positions
return (num & (num - 1)) == 0 and (num & 0x55555555) != 0
# Test the function
test_cases = [1, 4, 8, 16, 32, 64, 128, 256]
for num in test_cases:
result = is_power_of_4_bitwise(num)
print(f"{num}: {'Power of 4' if result else 'Not power of 4'}")
1: Power of 4 4: Power of 4 8: Not power of 4 16: Power of 4 32: Not power of 4 64: Power of 4 128: Not power of 4 256: Power of 4
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Division/Modulus | O(log n) | O(1) | Easy to understand |
| Logarithms | O(1) | O(1) | Mathematical approach |
| Bit Manipulation | O(1) | O(1) | Fastest execution |
How It Works
The division method works by repeatedly dividing the number by 4. If at any point there's a remainder, the number is not a power of 4. The logarithm method calculates log4(n) and checks if the result is an integer. The bitwise method uses the fact that powers of 4 have specific bit patterns.
Conclusion
Use the division method for clarity and educational purposes. The bitwise approach is most efficient for performance-critical applications. The logarithm method provides a mathematical solution but may have floating-point precision issues.
