Skip to content

Commit c0acfd4

Browse files
poyeagithub-actionscclauss
authored
Fix factorial issues (TheAlgorithms#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 <[email protected]> * Update factorial_iterative.py Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent 2e955ae commit c0acfd4

File tree

4 files changed

+22
-41
lines changed

4 files changed

+22
-41
lines changed

DIRECTORY.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
* [Morse Code](https://github.com/TheAlgorithms/Python/blob/master/ciphers/morse_code.py)
7676
* [Onepad Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/onepad_cipher.py)
7777
* [Playfair Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/playfair_cipher.py)
78+
* [Polybius](https://github.com/TheAlgorithms/Python/blob/master/ciphers/polybius.py)
7879
* [Porta Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/porta_cipher.py)
7980
* [Rabin Miller](https://github.com/TheAlgorithms/Python/blob/master/ciphers/rabin_miller.py)
8081
* [Rail Fence Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/rail_fence_cipher.py)
@@ -453,7 +454,6 @@
453454
* [Eulers Totient](https://github.com/TheAlgorithms/Python/blob/master/maths/eulers_totient.py)
454455
* [Extended Euclidean Algorithm](https://github.com/TheAlgorithms/Python/blob/master/maths/extended_euclidean_algorithm.py)
455456
* [Factorial Iterative](https://github.com/TheAlgorithms/Python/blob/master/maths/factorial_iterative.py)
456-
* [Factorial Python](https://github.com/TheAlgorithms/Python/blob/master/maths/factorial_python.py)
457457
* [Factorial Recursive](https://github.com/TheAlgorithms/Python/blob/master/maths/factorial_recursive.py)
458458
* [Factors](https://github.com/TheAlgorithms/Python/blob/master/maths/factors.py)
459459
* [Fermat Little Theorem](https://github.com/TheAlgorithms/Python/blob/master/maths/fermat_little_theorem.py)
@@ -565,6 +565,7 @@
565565

566566
## Other
567567
* [Activity Selection](https://github.com/TheAlgorithms/Python/blob/master/other/activity_selection.py)
568+
* [Check Strong Password](https://github.com/TheAlgorithms/Python/blob/master/other/check_strong_password.py)
568569
* [Date To Weekday](https://github.com/TheAlgorithms/Python/blob/master/other/date_to_weekday.py)
569570
* [Davisb Putnamb Logemannb Loveland](https://github.com/TheAlgorithms/Python/blob/master/other/davisb_putnamb_logemannb_loveland.py)
570571
* [Dijkstra Bankers Algorithm](https://github.com/TheAlgorithms/Python/blob/master/other/dijkstra_bankers_algorithm.py)
@@ -952,6 +953,7 @@
952953
* [Reverse Words](https://github.com/TheAlgorithms/Python/blob/master/strings/reverse_words.py)
953954
* [Split](https://github.com/TheAlgorithms/Python/blob/master/strings/split.py)
954955
* [Upper](https://github.com/TheAlgorithms/Python/blob/master/strings/upper.py)
956+
* [Wildcard Pattern Matching](https://github.com/TheAlgorithms/Python/blob/master/strings/wildcard_pattern_matching.py)
955957
* [Word Occurrence](https://github.com/TheAlgorithms/Python/blob/master/strings/word_occurrence.py)
956958
* [Word Patterns](https://github.com/TheAlgorithms/Python/blob/master/strings/word_patterns.py)
957959
* [Z Function](https://github.com/TheAlgorithms/Python/blob/master/strings/z_function.py)

maths/factorial_iterative.py

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
# factorial of a positive integer -- https://en.wikipedia.org/wiki/Factorial
1+
"""Factorial of a positive integer -- https://en.wikipedia.org/wiki/Factorial
2+
"""
23

34

4-
def factorial(n: int) -> int:
5+
def factorial(number: int) -> int:
56
"""
7+
Calculate the factorial of specified number (n!).
8+
69
>>> import math
710
>>> all(factorial(i) == math.factorial(i) for i in range(20))
811
True
@@ -14,17 +17,27 @@ def factorial(n: int) -> int:
1417
Traceback (most recent call last):
1518
...
1619
ValueError: factorial() not defined for negative values
20+
>>> factorial(1)
21+
1
22+
>>> factorial(6)
23+
720
24+
>>> factorial(0)
25+
1
1726
"""
18-
if n != int(n):
27+
if number != int(number):
1928
raise ValueError("factorial() only accepts integral values")
20-
if n < 0:
29+
if number < 0:
2130
raise ValueError("factorial() not defined for negative values")
2231
value = 1
23-
for i in range(1, n + 1):
32+
for i in range(1, number + 1):
2433
value *= i
2534
return value
2635

2736

2837
if __name__ == "__main__":
38+
import doctest
39+
40+
doctest.testmod()
41+
2942
n = int(input("Enter a positive integer: ").strip() or 0)
3043
print(f"factorial{n} is {factorial(n)}")

maths/factorial_python.py

-34
This file was deleted.

project_euler/problem_015/sol1.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def solution(n: int = 20) -> int:
2626
"""
2727
n = 2 * n # middle entry of odd rows starting at row 3 is the solution for n = 1,
2828
# 2, 3,...
29-
k = n / 2
29+
k = n // 2
3030

3131
return int(factorial(n) / (factorial(k) * factorial(n - k)))
3232

0 commit comments

Comments
 (0)