From 09f27a60f1389852760e39412d1719e863a411c0 Mon Sep 17 00:00:00 2001 From: Melove Gupta Date: Wed, 22 Oct 2025 13:20:24 +0530 Subject: [PATCH 1/2] Create is_power_of_four.py --- maths/is_power_of_four.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 maths/is_power_of_four.py diff --git a/maths/is_power_of_four.py b/maths/is_power_of_four.py new file mode 100644 index 000000000000..7fb0c615100c --- /dev/null +++ b/maths/is_power_of_four.py @@ -0,0 +1,37 @@ +""" +Check if an integer is a power of four. + +Time Complexity: O(logâ‚„ n) +Space Complexity: O(1) +""" + +def is_power_of_four(n: int) -> bool: + """Return True if n is a power of four (n > 0). + + >>> is_power_of_four(1) + True + >>> is_power_of_four(4) + True + >>> is_power_of_four(16) + True + >>> is_power_of_four(64) + True + >>> is_power_of_four(8) + False + >>> is_power_of_four(0) + False + >>> is_power_of_four(-4) + False + """ + if not isinstance(n, int): + raise TypeError("n must be an integer") + if n <= 0: + return False + while n % 4 == 0: + n //= 4 + return n == 1 + + +if __name__ == "__main__": + import doctest + doctest.testmod() From cf4fbf2988eb825bbe32e234dd89353a134952df Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 22 Oct 2025 07:51:43 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/is_power_of_four.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/maths/is_power_of_four.py b/maths/is_power_of_four.py index 7fb0c615100c..661e65446c65 100644 --- a/maths/is_power_of_four.py +++ b/maths/is_power_of_four.py @@ -5,6 +5,7 @@ Space Complexity: O(1) """ + def is_power_of_four(n: int) -> bool: """Return True if n is a power of four (n > 0). @@ -34,4 +35,5 @@ def is_power_of_four(n: int) -> bool: if __name__ == "__main__": import doctest + doctest.testmod()