1- """
2- == Automorphic Numbers ==
3- A number n is said to be a Automorphic number if
4- the square of n "ends" in the same digits as n itself.
5-
6- Examples of Automorphic Numbers: 0, 1, 5, 6, 25, 76, 376, 625, 9376, 90625, ...
7- https://en.wikipedia.org/wiki/Automorphic_number
8- """
1+ def is_automorphic_number (number : int , verbose : bool = False ) -> bool :
2+ """
3+ This function checks if a given number is an automorphic number.
94
10- # Author : Akshay Dubey (https://github.com/itsAkshayDubey)
11- # Time Complexity : O(log10n)
5+ Args:
6+ number (int): The number to check.
7+ verbose (bool): If True, prints the square of the number and the comparison result. Defaults to False.
128
9+ Returns:
10+ bool: True if the number is automorphic, False otherwise.
1311
14- def is_automorphic_number (number : int ) -> bool :
15- """
16- # doctest: +NORMALIZE_WHITESPACE
17- This functions takes an integer number as input.
18- returns True if the number is automorphic.
19- >>> is_automorphic_number(-1)
20- False
21- >>> is_automorphic_number(0)
22- True
23- >>> is_automorphic_number(5)
24- True
25- >>> is_automorphic_number(6)
26- True
27- >>> is_automorphic_number(7)
28- False
29- >>> is_automorphic_number(25)
30- True
31- >>> is_automorphic_number(259918212890625)
32- True
33- >>> is_automorphic_number(259918212890636)
34- False
35- >>> is_automorphic_number(740081787109376)
36- True
37- >>> is_automorphic_number(5.0)
38- Traceback (most recent call last):
39- ...
40- TypeError: Input value of [number=5.0] must be an integer
12+ Raises:
13+ TypeError: If the input number is not an integer.
14+ ValueError: If the input number is negative.
4115 """
4216 if not isinstance (number , int ):
4317 msg = f"Input value of [number={ number } ] must be an integer"
4418 raise TypeError (msg )
4519 if number < 0 :
46- return False
20+ msg = f"Input value of [number={ number } ] must be a non-negative integer"
21+ raise ValueError (msg )
4722 number_square = number * number
23+ if verbose :
24+ print (f"Square of { number } : { number_square } " )
4825 while number > 0 :
4926 if number % 10 != number_square % 10 :
27+ if verbose :
28+ print (f"{ number } is not an automorphic number" )
5029 return False
5130 number //= 10
5231 number_square //= 10
32+ if verbose :
33+ print (f"{ number } is an automorphic number" )
5334 return True
5435
5536
37+ def find_automorphic_numbers (n : int ) -> list :
38+ """
39+ This function finds all automorphic numbers up to a given number.
40+
41+ Args:
42+ n (int): The upper limit.
43+
44+ Returns:
45+ list: A list of automorphic numbers up to n.
46+ """
47+ automorphic_numbers = []
48+ for i in range (n + 1 ):
49+ if is_automorphic_number (i ):
50+ automorphic_numbers .append (i )
51+ return automorphic_numbers
52+
53+
5654if __name__ == "__main__" :
5755 import doctest
5856
5957 doctest .testmod ()
58+
59+ # Example usage:
60+ print (is_automorphic_number (25 , verbose = True ))
61+ print (find_automorphic_numbers (100 ))
0 commit comments