From 644c50021575294ac586663792c7fb0024e9ad8b Mon Sep 17 00:00:00 2001 From: Jordan Rinder Date: Sat, 8 Oct 2022 16:39:23 -0400 Subject: [PATCH 1/3] Add Catalan number to maths --- DIRECTORY.md | 1 + maths/catalan_number.py | 51 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 maths/catalan_number.py diff --git a/DIRECTORY.md b/DIRECTORY.md index 64e9d5333a2f..c3fded227cb7 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -475,6 +475,7 @@ * [Binomial Coefficient](maths/binomial_coefficient.py) * [Binomial Distribution](maths/binomial_distribution.py) * [Bisection](maths/bisection.py) + * [Catalan Number](maths/catalan_number.py) * [Ceil](maths/ceil.py) * [Check Polygon](maths/check_polygon.py) * [Chudnovsky Algorithm](maths/chudnovsky_algorithm.py) diff --git a/maths/catalan_number.py b/maths/catalan_number.py new file mode 100644 index 000000000000..3267ad966f33 --- /dev/null +++ b/maths/catalan_number.py @@ -0,0 +1,51 @@ +""" + +Calculate the nth Catalan number + +Source: + https://en.wikipedia.org/wiki/Catalan_number + +""" + + +def catalan(number: int) -> int: + """ + :param number: nth catalan number to calculate + :return: the nth catalan number + Note: A catalan number is only defined for positive integers + + >>> catalan(5) + 14 + >>> catalan(0) + Traceback (most recent call last): + ... + ValueError: Input value of [number=0] must be > 0 + >>> catalan(-1) + Traceback (most recent call last): + ... + ValueError: Input value of [number=-1] must be > 0 + >>> catalan(5.0) + Traceback (most recent call last): + ... + TypeError: Input value of [number=5.0] must be an integer + """ + + if not isinstance(number, int): + raise TypeError(f"Input value of [number={number}] must be an integer") + + if number < 1: + raise ValueError(f"Input value of [number={number}] must be > 0") + + current_number = 1 + + for i in range(1, number): + current_number *= (4 * i - 2) + current_number //= (i + 1) + + return current_number + + +if __name__ == '__main__': + import doctest + + doctest.testmod() From fe88a0b17db68e6b4d83237591d34786243f9623 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 8 Oct 2022 20:41:15 +0000 Subject: [PATCH 2/3] updating DIRECTORY.md --- DIRECTORY.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index c3fded227cb7..668da4761f74 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -475,7 +475,7 @@ * [Binomial Coefficient](maths/binomial_coefficient.py) * [Binomial Distribution](maths/binomial_distribution.py) * [Bisection](maths/bisection.py) - * [Catalan Number](maths/catalan_number.py) + * [Catalan Number](maths/catalan_number.py) * [Ceil](maths/ceil.py) * [Check Polygon](maths/check_polygon.py) * [Chudnovsky Algorithm](maths/chudnovsky_algorithm.py) @@ -633,8 +633,9 @@ ## Physics * [Horizontal Projectile Motion](physics/horizontal_projectile_motion.py) - * [Lorenz Transformation Four Vector](physics/lorenz_transformation_four_vector.py) + * [Lorentz Transformation Four Vector](physics/lorentz_transformation_four_vector.py) * [N Body Simulation](physics/n_body_simulation.py) + * [Newtons Law Of Gravitation](physics/newtons_law_of_gravitation.py) * [Newtons Second Law Of Motion](physics/newtons_second_law_of_motion.py) ## Project Euler From 1807be197155dec0008d4436f5cc0d8ef8c37780 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 8 Oct 2022 20:43:44 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/catalan_number.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maths/catalan_number.py b/maths/catalan_number.py index 3267ad966f33..4a1280a45bf2 100644 --- a/maths/catalan_number.py +++ b/maths/catalan_number.py @@ -39,13 +39,13 @@ def catalan(number: int) -> int: current_number = 1 for i in range(1, number): - current_number *= (4 * i - 2) - current_number //= (i + 1) + current_number *= 4 * i - 2 + current_number //= i + 1 return current_number -if __name__ == '__main__': +if __name__ == "__main__": import doctest doctest.testmod()