From c9c27d4de4cbfc9a6afe5a162ee6be2965760e0a Mon Sep 17 00:00:00 2001 From: Melove Gupta Date: Wed, 22 Oct 2025 13:13:23 +0530 Subject: [PATCH 1/2] Create is_power_of_three.py --- maths/is_power_of_three.py | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 maths/is_power_of_three.py diff --git a/maths/is_power_of_three.py b/maths/is_power_of_three.py new file mode 100644 index 000000000000..8f1151a0258d --- /dev/null +++ b/maths/is_power_of_three.py @@ -0,0 +1,39 @@ +""" +Check if an integer is a power of three. + +Time Complexity: O(log₃ n) +Space Complexity: O(1) +""" + +def is_power_of_three(n: int) -> bool: + """Return True if n is a power of three (n > 0). + + >>> is_power_of_three(1) + True + >>> is_power_of_three(3) + True + >>> is_power_of_three(9) + True + >>> is_power_of_three(27) + True + >>> is_power_of_three(2) + False + >>> is_power_of_three(0) + False + >>> is_power_of_three(-3) + False + >>> is_power_of_three(81) + True + """ + if not isinstance(n, int): + raise TypeError("n must be an integer") + if n <= 0: + return False + while n % 3 == 0: + n //= 3 + return n == 1 + + +if __name__ == "__main__": + import doctest + doctest.testmod() From 6cdf9689ea3c67e4546c7e9a9aef6a00f8cf9527 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:45:29 +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_three.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/maths/is_power_of_three.py b/maths/is_power_of_three.py index 8f1151a0258d..6450b8784319 100644 --- a/maths/is_power_of_three.py +++ b/maths/is_power_of_three.py @@ -5,6 +5,7 @@ Space Complexity: O(1) """ + def is_power_of_three(n: int) -> bool: """Return True if n is a power of three (n > 0). @@ -36,4 +37,5 @@ def is_power_of_three(n: int) -> bool: if __name__ == "__main__": import doctest + doctest.testmod()