Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,7 @@
* Forecasting
* [Run](machine_learning/forecasting/run.py)
* [Frequent Pattern Growth](machine_learning/frequent_pattern_growth.py)
* [Gaussian Mixture Model](machine_learning/gaussian_mixture_model.py)
* [Gaussian Naive Bayes](machine_learning/gaussian_naive_bayes.py)
* [Gradient Boosting Classifier](machine_learning/gradient_boosting_classifier.py)
* [Gradient Boosting Regressor](machine_learning/gradient_boosting_regressor.py)
Expand Down Expand Up @@ -742,6 +743,7 @@
* [Bailey Borwein Plouffe](maths/bailey_borwein_plouffe.py)
* [Base Neg2 Conversion](maths/base_neg2_conversion.py)
* [Basic Maths](maths/basic_maths.py)
* [Bearing](maths/bearing.py)
* [Binary Exponentiation](maths/binary_exponentiation.py)
* [Binary Multiplication](maths/binary_multiplication.py)
* [Binomial Coefficient](maths/binomial_coefficient.py)
Expand Down Expand Up @@ -878,6 +880,7 @@
* [Hexagonal Numbers](maths/series/hexagonal_numbers.py)
* [Logarithmic Series](maths/series/logarithmic_series.py)
* [P Series](maths/series/p_series.py)
* [Shoelace Area](maths/shoelace_area.py)
* [Sieve Of Atkin](maths/sieve_of_atkin.py)
* [Sieve Of Eratosthenes](maths/sieve_of_eratosthenes.py)
* [Sigmoid](maths/sigmoid.py)
Expand Down Expand Up @@ -909,6 +912,7 @@
* [Pronic Number](maths/special_numbers/pronic_number.py)
* [Proth Number](maths/special_numbers/proth_number.py)
* [Triangular Numbers](maths/special_numbers/triangular_numbers.py)
* [Trimorphic Number](maths/special_numbers/trimorphic_number.py)
* [Ugly Numbers](maths/special_numbers/ugly_numbers.py)
* [Weird Number](maths/special_numbers/weird_number.py)
* [Sum Of Arithmetic Series](maths/sum_of_arithmetic_series.py)
Expand Down
53 changes: 53 additions & 0 deletions maths/special_numbers/trimorphic_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
A Trimorphic Number is a number whose cube ends in the same digits as the number itself.
For example, 4^3 = 64, which ends in 4.

Reference: https://en.wikipedia.org/wiki/Automorphic_number#Trimorphic_numbers
"""


def is_trimorphic(number: int) -> bool:
Comment thread
Diogenis15 marked this conversation as resolved.
Comment thread
Diogenis15 marked this conversation as resolved.
"""
Checks if a number is a Trimorphic number.

:param number: The number to check
:return: True if the number is trimorphic, False otherwise
:raises ValueError: If the input number is negative

>>> is_trimorphic(0)
True
>>> is_trimorphic(1)
True
>>> is_trimorphic(4)
True
>>> is_trimorphic(5)
True
>>> is_trimorphic(24)
True
>>> is_trimorphic(249)
True
>>> is_trimorphic(2)
False
>>> is_trimorphic(7)
False
>>> is_trimorphic(10)
False
>>> is_trimorphic(-1)
Traceback (most recent call last):
...
ValueError: Input must be a non-negative integer
"""
if number < 0:
raise ValueError("Input must be a non-negative integer")

if number == 0:
return True

cube = number**3
return str(cube).endswith(str(number))


if __name__ == "__main__":
import doctest

doctest.testmod()