From 83f185ad29f6e69a8908b5c86efaa88573a6fd86 Mon Sep 17 00:00:00 2001 From: Sourabhyadav09 <146635956+Sourabhyadav09@users.noreply.github.com> Date: Thu, 30 Oct 2025 01:50:51 +0530 Subject: [PATCH 1/2] Added factorial_recursive.py with doctests and descriptive parameter name --- maths/factorial_recursive.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 maths/factorial_recursive.py diff --git a/maths/factorial_recursive.py b/maths/factorial_recursive.py new file mode 100644 index 000000000000..1aacafeacc11 --- /dev/null +++ b/maths/factorial_recursive.py @@ -0,0 +1,36 @@ +""" +Recursive factorial implementation. + +Reference: https://en.wikipedia.org/wiki/Factorial +""" + +def factorial(number: int) -> int: + """ + Calculate the factorial of a non-negative integer recursively. + + Parameters: + number (int): A non-negative integer whose factorial is to be calculated. + + Returns: + int: The factorial of the input number. + + Raises: + ValueError: If the input number is negative. + + Examples: + >>> factorial(0) + 1 + >>> factorial(1) + 1 + >>> factorial(5) + 120 + """ + if number < 0: + raise ValueError("number must be non-negative") + if number <= 1: + return 1 + return number * factorial(number - 1) + + +if __name__ == "__main__": + print(factorial(5)) From 23956feb88a19807e7ffa343d943779afd07bac4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 29 Oct 2025 20:24:54 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/factorial_recursive.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/factorial_recursive.py b/maths/factorial_recursive.py index 1aacafeacc11..dd7d2b860722 100644 --- a/maths/factorial_recursive.py +++ b/maths/factorial_recursive.py @@ -4,6 +4,7 @@ Reference: https://en.wikipedia.org/wiki/Factorial """ + def factorial(number: int) -> int: """ Calculate the factorial of a non-negative integer recursively.