mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Fix factorial issues (#5496)
* updating DIRECTORY.md * pass integer to `math.factorial` in `project_euler/problem_015` * remove duplicated factorial function * updating DIRECTORY.md * Update maths/factorial_iterative.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update factorial_iterative.py Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
2e955aea46
commit
c0acfd46cb
|
@ -75,6 +75,7 @@
|
|||
* [Morse Code](https://github.com/TheAlgorithms/Python/blob/master/ciphers/morse_code.py)
|
||||
* [Onepad Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/onepad_cipher.py)
|
||||
* [Playfair Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/playfair_cipher.py)
|
||||
* [Polybius](https://github.com/TheAlgorithms/Python/blob/master/ciphers/polybius.py)
|
||||
* [Porta Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/porta_cipher.py)
|
||||
* [Rabin Miller](https://github.com/TheAlgorithms/Python/blob/master/ciphers/rabin_miller.py)
|
||||
* [Rail Fence Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/rail_fence_cipher.py)
|
||||
|
@ -453,7 +454,6 @@
|
|||
* [Eulers Totient](https://github.com/TheAlgorithms/Python/blob/master/maths/eulers_totient.py)
|
||||
* [Extended Euclidean Algorithm](https://github.com/TheAlgorithms/Python/blob/master/maths/extended_euclidean_algorithm.py)
|
||||
* [Factorial Iterative](https://github.com/TheAlgorithms/Python/blob/master/maths/factorial_iterative.py)
|
||||
* [Factorial Python](https://github.com/TheAlgorithms/Python/blob/master/maths/factorial_python.py)
|
||||
* [Factorial Recursive](https://github.com/TheAlgorithms/Python/blob/master/maths/factorial_recursive.py)
|
||||
* [Factors](https://github.com/TheAlgorithms/Python/blob/master/maths/factors.py)
|
||||
* [Fermat Little Theorem](https://github.com/TheAlgorithms/Python/blob/master/maths/fermat_little_theorem.py)
|
||||
|
@ -565,6 +565,7 @@
|
|||
|
||||
## Other
|
||||
* [Activity Selection](https://github.com/TheAlgorithms/Python/blob/master/other/activity_selection.py)
|
||||
* [Check Strong Password](https://github.com/TheAlgorithms/Python/blob/master/other/check_strong_password.py)
|
||||
* [Date To Weekday](https://github.com/TheAlgorithms/Python/blob/master/other/date_to_weekday.py)
|
||||
* [Davisb Putnamb Logemannb Loveland](https://github.com/TheAlgorithms/Python/blob/master/other/davisb_putnamb_logemannb_loveland.py)
|
||||
* [Dijkstra Bankers Algorithm](https://github.com/TheAlgorithms/Python/blob/master/other/dijkstra_bankers_algorithm.py)
|
||||
|
@ -952,6 +953,7 @@
|
|||
* [Reverse Words](https://github.com/TheAlgorithms/Python/blob/master/strings/reverse_words.py)
|
||||
* [Split](https://github.com/TheAlgorithms/Python/blob/master/strings/split.py)
|
||||
* [Upper](https://github.com/TheAlgorithms/Python/blob/master/strings/upper.py)
|
||||
* [Wildcard Pattern Matching](https://github.com/TheAlgorithms/Python/blob/master/strings/wildcard_pattern_matching.py)
|
||||
* [Word Occurrence](https://github.com/TheAlgorithms/Python/blob/master/strings/word_occurrence.py)
|
||||
* [Word Patterns](https://github.com/TheAlgorithms/Python/blob/master/strings/word_patterns.py)
|
||||
* [Z Function](https://github.com/TheAlgorithms/Python/blob/master/strings/z_function.py)
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
# factorial of a positive integer -- https://en.wikipedia.org/wiki/Factorial
|
||||
"""Factorial of a positive integer -- https://en.wikipedia.org/wiki/Factorial
|
||||
"""
|
||||
|
||||
|
||||
def factorial(n: int) -> int:
|
||||
def factorial(number: int) -> int:
|
||||
"""
|
||||
Calculate the factorial of specified number (n!).
|
||||
|
||||
>>> import math
|
||||
>>> all(factorial(i) == math.factorial(i) for i in range(20))
|
||||
True
|
||||
|
@ -14,17 +17,27 @@ def factorial(n: int) -> int:
|
|||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: factorial() not defined for negative values
|
||||
>>> factorial(1)
|
||||
1
|
||||
>>> factorial(6)
|
||||
720
|
||||
>>> factorial(0)
|
||||
1
|
||||
"""
|
||||
if n != int(n):
|
||||
if number != int(number):
|
||||
raise ValueError("factorial() only accepts integral values")
|
||||
if n < 0:
|
||||
if number < 0:
|
||||
raise ValueError("factorial() not defined for negative values")
|
||||
value = 1
|
||||
for i in range(1, n + 1):
|
||||
for i in range(1, number + 1):
|
||||
value *= i
|
||||
return value
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
||||
|
||||
n = int(input("Enter a positive integer: ").strip() or 0)
|
||||
print(f"factorial{n} is {factorial(n)}")
|
||||
|
|
|
@ -1,34 +0,0 @@
|
|||
def factorial(input_number: int) -> int:
|
||||
"""
|
||||
Calculate the factorial of specified number
|
||||
|
||||
>>> factorial(1)
|
||||
1
|
||||
>>> factorial(6)
|
||||
720
|
||||
>>> factorial(0)
|
||||
1
|
||||
>>> factorial(-1)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: factorial() not defined for negative values
|
||||
>>> factorial(0.1)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: factorial() only accepts integral values
|
||||
"""
|
||||
|
||||
if input_number < 0:
|
||||
raise ValueError("factorial() not defined for negative values")
|
||||
if not isinstance(input_number, int):
|
||||
raise ValueError("factorial() only accepts integral values")
|
||||
result = 1
|
||||
for i in range(1, input_number):
|
||||
result = result * (i + 1)
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
|
@ -26,7 +26,7 @@ def solution(n: int = 20) -> int:
|
|||
"""
|
||||
n = 2 * n # middle entry of odd rows starting at row 3 is the solution for n = 1,
|
||||
# 2, 3,...
|
||||
k = n / 2
|
||||
k = n // 2
|
||||
|
||||
return int(factorial(n) / (factorial(k) * factorial(n - k)))
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user