Skip to content

Create is_power_of_four_logarithm.py #9337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions maths/is_power_of_four_logarithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import math


def is_power_of_four_logarithm(num: int) -> bool:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file maths/is_power_of_four_logarithm.py, please provide doctest for the function is_power_of_four_logarithm

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file maths/is_power_of_four_logarithm.py, please provide doctest for the function is_power_of_four_logarithm

"""
Check if a given number is a power of four using logarithms.

Args:
num (int): The number to be checked.

Returns:
bool: True if the number is a power of 4, False otherwise.

Raises:
ValueError: If the input number is not positive.
"""
if num <= 0:
raise ValueError("Input number must be positive")

# Calculate the logarithm base 4 of the number
log_base_4 = math.log(num, 4)

# Check if the result is an integer
return log_base_4.is_integer()


# Test cases
if __name__ == "__main__":
num1 = 16 # 4^2 = 16
num2 = 4096 # 4^6 = 4096
num3 = 18 # Not a power of 4

print(f"{num1} is a power of 4: {is_power_of_four_logarithm(num1)}")
print(f"{num2} is a power of 4: {is_power_of_four_logarithm(num2)}")
print(f"{num3} is a power of 4: {is_power_of_four_logarithm(num3)}")