Skip to content

Commit c853e30

Browse files
Add automorphic number check algorithm
1 parent 8934bab commit c853e30

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

maths/automorphic_number.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def is_automorphic(n: int) -> bool:
2+
"""
3+
Check if a number is an Automorphic Number.
4+
A number is automorphic if its square ends with the number itself.
5+
6+
Examples:
7+
>>> is_automorphic(5)
8+
True
9+
>>> is_automorphic(6)
10+
True
11+
>>> is_automorphic(76)
12+
True
13+
>>> is_automorphic(7)
14+
False
15+
16+
Time Complexity: O(d) where d is number of digits
17+
"""
18+
square = n * n
19+
return str(square).endswith(str(n))
20+
21+
22+
if __name__ == "__main__":
23+
import doctest
24+
doctest.testmod()

0 commit comments

Comments
 (0)