Skip to content

Commit fe7f974

Browse files
authored
Add implementation for Trimorphic Number
An algorithm that checks if a number is trimorphic, which is a number whose cube ends with the same digits as the number itself
1 parent 8106aea commit fe7f974

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

trimorphic_number.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
A Trimorphic Number is a number whose cube ends in the same digits as the number itself.
3+
For example, 4^3 = 64, which ends in 4.
4+
5+
Reference: https://en.wikipedia.org/wiki/Automorphic_number#Trimorphic_numbers
6+
"""
7+
8+
9+
def is_trimorphic(number: int) -> bool:
10+
"""
11+
Checks if a number is a Trimorphic number.
12+
"""
13+
if number < 0:
14+
raise ValueError("Input must be a non-negative integer")
15+
16+
if number == 0:
17+
return True
18+
19+
cube = number**3
20+
return str(cube).endswith(str(number))
21+
22+
23+
if __name__ == "__main__":
24+
import doctest
25+
26+
doctest.testmod()

0 commit comments

Comments
 (0)