Skip to content

Commit 9a51bf9

Browse files
Diogenis15cclauss
andauthored
Implementation for Trimorphic Number (#14229)
* 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 * Add doctests for is_trimorphic function Added doctests for the is_trimorphic function to validate its behavior with various inputs, including edge cases. * Enhance docstring for is_trimorphic function Added parameter and return type documentation to is_trimorphic function. * Rename trimorphic_number.py to maths/special_numbers/trimorphic_number.py * updating DIRECTORY.md --------- Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: cclauss <cclauss@users.noreply.github.com>
1 parent 1ac05c7 commit 9a51bf9

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

DIRECTORY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@
695695
* Forecasting
696696
* [Run](machine_learning/forecasting/run.py)
697697
* [Frequent Pattern Growth](machine_learning/frequent_pattern_growth.py)
698+
* [Gaussian Mixture Model](machine_learning/gaussian_mixture_model.py)
698699
* [Gaussian Naive Bayes](machine_learning/gaussian_naive_bayes.py)
699700
* [Gradient Boosting Classifier](machine_learning/gradient_boosting_classifier.py)
700701
* [Gradient Boosting Regressor](machine_learning/gradient_boosting_regressor.py)
@@ -742,6 +743,7 @@
742743
* [Bailey Borwein Plouffe](maths/bailey_borwein_plouffe.py)
743744
* [Base Neg2 Conversion](maths/base_neg2_conversion.py)
744745
* [Basic Maths](maths/basic_maths.py)
746+
* [Bearing](maths/bearing.py)
745747
* [Binary Exponentiation](maths/binary_exponentiation.py)
746748
* [Binary Multiplication](maths/binary_multiplication.py)
747749
* [Binomial Coefficient](maths/binomial_coefficient.py)
@@ -878,6 +880,7 @@
878880
* [Hexagonal Numbers](maths/series/hexagonal_numbers.py)
879881
* [Logarithmic Series](maths/series/logarithmic_series.py)
880882
* [P Series](maths/series/p_series.py)
883+
* [Shoelace Area](maths/shoelace_area.py)
881884
* [Sieve Of Atkin](maths/sieve_of_atkin.py)
882885
* [Sieve Of Eratosthenes](maths/sieve_of_eratosthenes.py)
883886
* [Sigmoid](maths/sigmoid.py)
@@ -909,6 +912,7 @@
909912
* [Pronic Number](maths/special_numbers/pronic_number.py)
910913
* [Proth Number](maths/special_numbers/proth_number.py)
911914
* [Triangular Numbers](maths/special_numbers/triangular_numbers.py)
915+
* [Trimorphic Number](maths/special_numbers/trimorphic_number.py)
912916
* [Ugly Numbers](maths/special_numbers/ugly_numbers.py)
913917
* [Weird Number](maths/special_numbers/weird_number.py)
914918
* [Sum Of Arithmetic Series](maths/sum_of_arithmetic_series.py)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
:param number: The number to check
14+
:return: True if the number is trimorphic, False otherwise
15+
:raises ValueError: If the input number is negative
16+
17+
>>> is_trimorphic(0)
18+
True
19+
>>> is_trimorphic(1)
20+
True
21+
>>> is_trimorphic(4)
22+
True
23+
>>> is_trimorphic(5)
24+
True
25+
>>> is_trimorphic(24)
26+
True
27+
>>> is_trimorphic(249)
28+
True
29+
>>> is_trimorphic(2)
30+
False
31+
>>> is_trimorphic(7)
32+
False
33+
>>> is_trimorphic(10)
34+
False
35+
>>> is_trimorphic(-1)
36+
Traceback (most recent call last):
37+
...
38+
ValueError: Input must be a non-negative integer
39+
"""
40+
if number < 0:
41+
raise ValueError("Input must be a non-negative integer")
42+
43+
if number == 0:
44+
return True
45+
46+
cube = number**3
47+
return str(cube).endswith(str(number))
48+
49+
50+
if __name__ == "__main__":
51+
import doctest
52+
53+
doctest.testmod()

0 commit comments

Comments
 (0)