Skip to content

Commit 59db106

Browse files
jiwoogitgoswami-rahul
authored andcommitted
Created factorial.py in algorithms/maths (keon#309)
* Create factorial.py * Update __init__.py * Update test_maths.py * Update test_maths.py I added TestFactorial, also modified TestCombination comment. * Update README.md * Update README_CN.md * Update README_GE.md * Update README_JP.md * Update README_KR.md * Update test_maths.py * Update factorial.py
1 parent f88e499 commit 59db106

File tree

8 files changed

+48
-1
lines changed

8 files changed

+48
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ If you want to uninstall algorithms, it is as simple as:
174174
- [base_conversion](algorithms/maths/base_conversion.py)
175175
- [combination](algorithms/maths/combination.py)
176176
- [extended_gcd](algorithms/maths/extended_gcd.py)
177+
- [factorial](algorithms/maths/factorial.py)
177178
- [gcd/lcm](algorithms/maths/gcd.py)
178179
- [generate_strobogrammtic](algorithms/maths/generate_strobogrammtic.py)
179180
- [is_strobogrammatic](algorithms/maths/is_strobogrammatic.py)

README_CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ pip3 uninstall -y algorithms
165165
- [math:数学问题](algorithms/maths)
166166
- [extended_gcd:扩展欧几里得算法](algorithms/maths/extended_gcd.py)
167167
- [combination](algorithms/maths/combination.py)
168+
- [factorial](algorithms/maths/factorial.py)
168169
- [gcd/lcm:最大公约数和最小公倍数](algorithms/maths/gcd.py)
169170
- [prime_test:主要测试](algorithms/maths/prime_test.py)
170171
- [primes_sieve_of_eratosthenes:埃拉托色尼的质数筛](algorithms/maths/primes_sieve_of_eratosthenes.py)

README_GE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ Um das Projekt zu deinstallieren tippen Sie folgendes:
181181
- [base_conversion](algorithms/maths/base_conversion.py)
182182
- [combination](algorithms/maths/combination.py)
183183
- [extended_gcd](algorithms/maths/extended_gcd.py)
184+
- [factorial](algorithms/maths/factorial.py)
184185
- [gcd/lcm](algorithms/maths/gcd.py)
185186
- [generate_strobogrammtic](algorithms/maths/generate_strobogrammtic.py)
186187
- [is_strobogrammatic](algorithms/maths/is_strobogrammatic.py)

README_JP.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ if __name__ == "__main__":
171171
- [base_conversion](algorithms/maths/base_conversion.py)
172172
- [combination](algorithms/maths/combination.py)
173173
- [extended_gcd](algorithms/maths/extended_gcd.py)
174+
- [factorial](algorithms/maths/factorial.py)
174175
- [gcd/lcm](algorithms/maths/gcd.py)
175176
- [generate_strobogrammtic](algorithms/maths/generate_strobogrammtic.py)
176177
- [is_strobogrammatic](algorithms/maths/is_strobogrammatic.py)

README_KR.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ if __name__ == "__main__":
167167
- [base_conversion](algorithms/maths/base_conversion.py)
168168
- [combination](algorithms/maths/combination.py)
169169
- [extended_gcd](algorithms/maths/extended_gcd.py)
170+
- [factorial](algorithms/maths/factorial.py)
170171
- [gcd/lcm](algorithms/maths/gcd.py)
171172
- [generate_strobogrammtic](algorithms/maths/generate_strobogrammtic.py)
172173
- [is_strobogrammatic](algorithms/maths/is_strobogrammatic.py)

algorithms/maths/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from .base_conversion import *
22
from .extended_gcd import *
3+
from .factorial import *
34
from .gcd import *
45
from .generate_strobogrammtic import *
56
from .is_strobogrammatic import *

algorithms/maths/factorial.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# This function calculates n!
3+
# Factorial function not works in less than 0
4+
#
5+
6+
def factorial(n):
7+
8+
result = 1
9+
for i in range(2, n+1):
10+
result *= i
11+
12+
return result
13+
14+
15+
def factorial_recur(n):
16+
if n == 0:
17+
return 1
18+
19+
return n * factorial(n-1)
20+

tests/test_maths.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from algorithms.maths import (
22
int2base, base2int,
33
extended_gcd,
4+
factorial, factorial_recur,
45
gcd, lcm,
56
gen_strobogrammatic, strobogrammatic_in_range,
67
is_strobogrammatic, is_strobogrammatic2,
@@ -205,7 +206,7 @@ def test_encrypt_decrypt(self):
205206

206207
class TestCombination(unittest.TestCase):
207208
"""[summary]
208-
Test for the file rsa.py
209+
Test for the file combination.py
209210
210211
Arguments:
211212
unittest {[type]} -- [description]
@@ -215,5 +216,25 @@ def test_combination(self):
215216
self.assertEqual(10, combination(5, 2))
216217
self.assertEqual(252, combination(10, 5))
217218

219+
class TestFactorial(unittest.TestCase):
220+
"""[summary]
221+
Test for the file factorial.py
222+
223+
Arguments:
224+
unittest {[type]} -- [description]
225+
"""
226+
227+
def test_factorial(self):
228+
self.assertEqual(1, factorial(0))
229+
self.assertEqual(120, factorial(5))
230+
self.assertEqual(3628800, factorial(10))
231+
232+
def test_factorial_recur(self):
233+
self.assertEqual(1, factorial_recur(0))
234+
self.assertEqual(120, factorial_recur(5))
235+
self.assertEqual(3628800, factorial_recur(10))
236+
218237
if __name__ == "__main__":
219238
unittest.main()
239+
240+

0 commit comments

Comments
 (0)