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
3 changes: 3 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,8 @@
* [Activity Selection](other/activity_selection.py)
* [Alternative List Arrange](other/alternative_list_arrange.py)
* [Bankers Algorithm](other/bankers_algorithm.py)
* [Cheap Progress](other/cheap_progress.py)
* [Dancing Links](other/dancing_links.py)
* [Davis Putnam Logemann Loveland](other/davis_putnam_logemann_loveland.py)
* [Doomsday](other/doomsday.py)
* [Fischer Yates Shuffle](other/fischer_yates_shuffle.py)
Expand All @@ -1051,6 +1053,7 @@
* [Scoring Algorithm](other/scoring_algorithm.py)
* [Sdes](other/sdes.py)
* [Sliding Window Maximum](other/sliding_window_maximum.py)
* [Token Bucket](other/token_bucket.py)
* [Tower Of Hanoi](other/tower_of_hanoi.py)
* [Word Search](other/word_search.py)

Expand Down
34 changes: 19 additions & 15 deletions maths/factorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@

def factorial(number: int) -> int:
"""
Calculate the factorial of specified number (n!).
Calculate the factorial of the specified number.

>>> import math
>>> all(factorial(i) == math.factorial(i) for i in range(20))
True
>>> factorial(0)
1
>>> factorial(1)
1
>>> factorial(5)
120
>>> factorial(0.1)
Traceback (most recent call last):
...
Expand All @@ -18,12 +24,6 @@ def factorial(number: int) -> int:
Traceback (most recent call last):
...
ValueError: factorial() not defined for negative values
>>> factorial(1)
1
>>> factorial(6)
720
>>> factorial(0)
1
"""
if number != int(number):
raise ValueError("factorial() only accepts integral values")
Expand All @@ -35,14 +35,19 @@ def factorial(number: int) -> int:
return value


def factorial_recursive(n: int) -> int:
def factorial_recursive(number: int) -> int:
"""
Calculate the factorial of a positive integer
https://en.wikipedia.org/wiki/Factorial
Calculate the factorial of a number using recursion.

>>> import math
>>> all(factorial_recursive(i) == math.factorial(i) for i in range(20))
True
>>> factorial_recursive(0)
1
>>> factorial_recursive(1)
1
>>> factorial_recursive(5)
120
>>> factorial_recursive(0.1)
Traceback (most recent call last):
...
Expand All @@ -52,17 +57,16 @@ def factorial_recursive(n: int) -> int:
...
ValueError: factorial_recursive() not defined for negative values
"""
if not isinstance(n, int):
if not isinstance(number, int):
raise ValueError("factorial_recursive() only accepts integral values")
if n < 0:
if number < 0:
raise ValueError("factorial_recursive() not defined for negative values")
return 1 if n in {0, 1} else n * factorial_recursive(n - 1)
return 1 if number in {0, 1} else number * factorial_recursive(number - 1)


if __name__ == "__main__":
import doctest

doctest.testmod()

n = int(input("Enter a positive integer: ").strip() or 0)
print(f"factorial{n} is {factorial(n)}")
print(f"{n = } {factorial(n) = } {factorial_recursive(n) = }")
Loading