From c853e30d3d0ff091cdd785ca5e3a527d0c6a666d Mon Sep 17 00:00:00 2001 From: ShreyasKudahalli Date: Sun, 7 Dec 2025 23:22:11 +0530 Subject: [PATCH 1/3] Add automorphic number check algorithm --- maths/automorphic_number.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 maths/automorphic_number.py diff --git a/maths/automorphic_number.py b/maths/automorphic_number.py new file mode 100644 index 000000000000..9265c7f09868 --- /dev/null +++ b/maths/automorphic_number.py @@ -0,0 +1,24 @@ +def is_automorphic(n: int) -> bool: + """ + Check if a number is an Automorphic Number. + A number is automorphic if its square ends with the number itself. + + Examples: + >>> is_automorphic(5) + True + >>> is_automorphic(6) + True + >>> is_automorphic(76) + True + >>> is_automorphic(7) + False + + Time Complexity: O(d) where d is number of digits + """ + square = n * n + return str(square).endswith(str(n)) + + +if __name__ == "__main__": + import doctest + doctest.testmod() From 3645e14ed25fa512e6a637f79d1bfa75402d92f0 Mon Sep 17 00:00:00 2001 From: ShreyasKudahalli Date: Sun, 7 Dec 2025 23:25:42 +0530 Subject: [PATCH 2/3] Add automorphic number check algorithm --- maths/automorphic_number.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/maths/automorphic_number.py b/maths/automorphic_number.py index 9265c7f09868..262c77205e58 100644 --- a/maths/automorphic_number.py +++ b/maths/automorphic_number.py @@ -1,8 +1,15 @@ def is_automorphic(n: int) -> bool: """ Check if a number is an Automorphic Number. + A number is automorphic if its square ends with the number itself. + Args: + n: A non-negative integer. + + Returns: + True if n is automorphic, False otherwise. + Examples: >>> is_automorphic(5) True @@ -12,9 +19,17 @@ def is_automorphic(n: int) -> bool: True >>> is_automorphic(7) False + >>> is_automorphic(-5) + Traceback (most recent call last): + ... + ValueError: n must be a non-negative integer - Time Complexity: O(d) where d is number of digits + Time Complexity: + O(d) where d is the number of digits of n. """ + if n < 0: + raise ValueError("n must be a non-negative integer") + square = n * n return str(square).endswith(str(n)) From 9c45d8d497f073755e10b636e31be81d51e7323e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 7 Dec 2025 17:57:22 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/automorphic_number.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/automorphic_number.py b/maths/automorphic_number.py index 262c77205e58..96f7019f9e93 100644 --- a/maths/automorphic_number.py +++ b/maths/automorphic_number.py @@ -36,4 +36,5 @@ def is_automorphic(n: int) -> bool: if __name__ == "__main__": import doctest + doctest.testmod()