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.
9
4
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.
12
8
9
+ Returns:
10
+ bool: True if the number is automorphic, False otherwise.
13
11
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.
41
15
"""
42
16
if not isinstance (number , int ):
43
17
msg = f"Input value of [number={ number } ] must be an integer"
44
18
raise TypeError (msg )
45
19
if number < 0 :
46
- return False
20
+ msg = f"Input value of [number={ number } ] must be a non-negative integer"
21
+ raise ValueError (msg )
47
22
number_square = number * number
23
+ if verbose :
24
+ print (f"Square of { number } : { number_square } " )
48
25
while number > 0 :
49
26
if number % 10 != number_square % 10 :
27
+ if verbose :
28
+ print (f"{ number } is not an automorphic number" )
50
29
return False
51
30
number //= 10
52
31
number_square //= 10
32
+ if verbose :
33
+ print (f"{ number } is an automorphic number" )
53
34
return True
54
35
55
36
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
+
56
54
if __name__ == "__main__" :
57
55
import doctest
58
56
59
57
doctest .testmod ()
58
+
59
+ # Example usage:
60
+ print (is_automorphic_number (25 , verbose = True ))
61
+ print (find_automorphic_numbers (100 ))
0 commit comments