-
-
Notifications
You must be signed in to change notification settings - Fork 50.7k
Expand file tree
/
Copy pathautomorphic_number.py
More file actions
40 lines (31 loc) · 846 Bytes
/
automorphic_number.py
File metadata and controls
40 lines (31 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
>>> is_automorphic(6)
True
>>> is_automorphic(76)
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 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))
if __name__ == "__main__":
import doctest
doctest.testmod()