Skip to content

Commit 3645e14

Browse files
Add automorphic number check algorithm
1 parent c853e30 commit 3645e14

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

maths/automorphic_number.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
def is_automorphic(n: int) -> bool:
22
"""
33
Check if a number is an Automorphic Number.
4+
45
A number is automorphic if its square ends with the number itself.
56
7+
Args:
8+
n: A non-negative integer.
9+
10+
Returns:
11+
True if n is automorphic, False otherwise.
12+
613
Examples:
714
>>> is_automorphic(5)
815
True
@@ -12,9 +19,17 @@ def is_automorphic(n: int) -> bool:
1219
True
1320
>>> is_automorphic(7)
1421
False
22+
>>> is_automorphic(-5)
23+
Traceback (most recent call last):
24+
...
25+
ValueError: n must be a non-negative integer
1526
16-
Time Complexity: O(d) where d is number of digits
27+
Time Complexity:
28+
O(d) where d is the number of digits of n.
1729
"""
30+
if n < 0:
31+
raise ValueError("n must be a non-negative integer")
32+
1833
square = n * n
1934
return str(square).endswith(str(n))
2035

0 commit comments

Comments
 (0)