From 3b1347d01fc779813f00408460eb0a52ee9d2af3 Mon Sep 17 00:00:00 2001 From: Lakshya747 Date: Thu, 3 Oct 2024 12:07:52 +0530 Subject: [PATCH 1/3] Updated automorphic.py --- maths/special_numbers/automorphic_number.py | 78 +++++++++++---------- 1 file changed, 40 insertions(+), 38 deletions(-) diff --git a/maths/special_numbers/automorphic_number.py b/maths/special_numbers/automorphic_number.py index 8ed9375632a4..45f5d4d0fd77 100644 --- a/maths/special_numbers/automorphic_number.py +++ b/maths/special_numbers/automorphic_number.py @@ -1,59 +1,61 @@ -""" -== Automorphic Numbers == -A number n is said to be a Automorphic number if -the square of n "ends" in the same digits as n itself. - -Examples of Automorphic Numbers: 0, 1, 5, 6, 25, 76, 376, 625, 9376, 90625, ... -https://en.wikipedia.org/wiki/Automorphic_number -""" +def is_automorphic_number(number: int, verbose: bool = False) -> bool: + """ + This function checks if a given number is an automorphic number. -# Author : Akshay Dubey (https://github.com/itsAkshayDubey) -# Time Complexity : O(log10n) + Args: + number (int): The number to check. + verbose (bool): If True, prints the square of the number and the comparison result. Defaults to False. + Returns: + bool: True if the number is automorphic, False otherwise. -def is_automorphic_number(number: int) -> bool: - """ - # doctest: +NORMALIZE_WHITESPACE - This functions takes an integer number as input. - returns True if the number is automorphic. - >>> is_automorphic_number(-1) - False - >>> is_automorphic_number(0) - True - >>> is_automorphic_number(5) - True - >>> is_automorphic_number(6) - True - >>> is_automorphic_number(7) - False - >>> is_automorphic_number(25) - True - >>> is_automorphic_number(259918212890625) - True - >>> is_automorphic_number(259918212890636) - False - >>> is_automorphic_number(740081787109376) - True - >>> is_automorphic_number(5.0) - Traceback (most recent call last): - ... - TypeError: Input value of [number=5.0] must be an integer + Raises: + TypeError: If the input number is not an integer. + ValueError: If the input number is negative. """ if not isinstance(number, int): msg = f"Input value of [number={number}] must be an integer" raise TypeError(msg) if number < 0: - return False + msg = f"Input value of [number={number}] must be a non-negative integer" + raise ValueError(msg) number_square = number * number + if verbose: + print(f"Square of {number}: {number_square}") while number > 0: if number % 10 != number_square % 10: + if verbose: + print(f"{number} is not an automorphic number") return False number //= 10 number_square //= 10 + if verbose: + print(f"{number} is an automorphic number") return True +def find_automorphic_numbers(n: int) -> list: + """ + This function finds all automorphic numbers up to a given number. + + Args: + n (int): The upper limit. + + Returns: + list: A list of automorphic numbers up to n. + """ + automorphic_numbers = [] + for i in range(n + 1): + if is_automorphic_number(i): + automorphic_numbers.append(i) + return automorphic_numbers + + if __name__ == "__main__": import doctest doctest.testmod() + + # Example usage: + print(is_automorphic_number(25, verbose=True)) + print(find_automorphic_numbers(100)) \ No newline at end of file From 357c534ff2a46c7ed5c71da1b32fdf4f905ee389 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 3 Oct 2024 06:42:08 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/special_numbers/automorphic_number.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/special_numbers/automorphic_number.py b/maths/special_numbers/automorphic_number.py index 45f5d4d0fd77..b5d8cf282262 100644 --- a/maths/special_numbers/automorphic_number.py +++ b/maths/special_numbers/automorphic_number.py @@ -58,4 +58,4 @@ def find_automorphic_numbers(n: int) -> list: # Example usage: print(is_automorphic_number(25, verbose=True)) - print(find_automorphic_numbers(100)) \ No newline at end of file + print(find_automorphic_numbers(100)) From 8af16fc0b682189c366d990b2d2bfe305f53e62e Mon Sep 17 00:00:00 2001 From: Lakshya747 Date: Fri, 4 Oct 2024 22:32:45 +0530 Subject: [PATCH 3/3] Update automorphic_number.py --- maths/special_numbers/automorphic_number.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/maths/special_numbers/automorphic_number.py b/maths/special_numbers/automorphic_number.py index b5d8cf282262..af27b307d798 100644 --- a/maths/special_numbers/automorphic_number.py +++ b/maths/special_numbers/automorphic_number.py @@ -2,10 +2,6 @@ def is_automorphic_number(number: int, verbose: bool = False) -> bool: """ This function checks if a given number is an automorphic number. - Args: - number (int): The number to check. - verbose (bool): If True, prints the square of the number and the comparison result. Defaults to False. - Returns: bool: True if the number is automorphic, False otherwise.