Repository: keon/algorithms
Pull request #309 (2018-06-05 15:34:45)

Created factorial.py in algorithms/maths
I also modified a TestCombination comment

wldn0202: I put space around "=="

Patch:
diff --git a/README.md b/README.md
index 3c9cefe..c3fcb0a 100644
--- a/README.md
+++ b/README.md
@@ -174,6 +174,7 @@ If you want to uninstall algorithms, it is as simple as:
     - [base_conversion](algorithms/maths/base_conversion.py)
     - [combination](algorithms/maths/combination.py)
     - [extended_gcd](algorithms/maths/extended_gcd.py)
+    - [factorial](algorithms/maths/factorial.py)    
     - [gcd/lcm](algorithms/maths/gcd.py)
     - [generate_strobogrammtic](algorithms/maths/generate_strobogrammtic.py)
     - [is_strobogrammatic](algorithms/maths/is_strobogrammatic.py)
diff --git a/README_CN.md b/README_CN.md
index 8f7aee0..e1a866f 100644
--- a/README_CN.md
+++ b/README_CN.md
@@ -165,6 +165,7 @@ pip3 uninstall -y algorithms
 - [math：数学问题](algorithms/maths)
     - [extended_gcd：扩展欧几里得算法](algorithms/maths/extended_gcd.py)
     - [combination](algorithms/maths/combination.py)
+    - [factorial](algorithms/maths/factorial.py)
     - [gcd/lcm：最大公约数和最小公倍数](algorithms/maths/gcd.py)
     - [prime_test：主要测试](algorithms/maths/prime_test.py)
     - [primes_sieve_of_eratosthenes：埃拉托色尼的质数筛](algorithms/maths/primes_sieve_of_eratosthenes.py)
diff --git a/README_GE.md b/README_GE.md
index da3272b..17cb148 100644
--- a/README_GE.md
+++ b/README_GE.md
@@ -181,6 +181,7 @@ Um das Projekt zu deinstallieren tippen Sie folgendes:
     - [base_conversion](algorithms/maths/base_conversion.py)
     - [combination](algorithms/maths/combination.py)
     - [extended_gcd](algorithms/maths/extended_gcd.py)
+    - [factorial](algorithms/maths/factorial.py)
     - [gcd/lcm](algorithms/maths/gcd.py)
     - [generate_strobogrammtic](algorithms/maths/generate_strobogrammtic.py)
     - [is_strobogrammatic](algorithms/maths/is_strobogrammatic.py)
diff --git a/README_JP.md b/README_JP.md
index 9eb25e0..043c4bc 100644
--- a/README_JP.md
+++ b/README_JP.md
@@ -171,6 +171,7 @@ if __name__ == "__main__":
     - [base_conversion](algorithms/maths/base_conversion.py)
     - [combination](algorithms/maths/combination.py)
     - [extended_gcd](algorithms/maths/extended_gcd.py)
+    - [factorial](algorithms/maths/factorial.py)
     - [gcd/lcm](algorithms/maths/gcd.py)
     - [generate_strobogrammtic](algorithms/maths/generate_strobogrammtic.py)
     - [is_strobogrammatic](algorithms/maths/is_strobogrammatic.py)
diff --git a/README_KR.md b/README_KR.md
index 7dd07f2..45e2eaf 100644
--- a/README_KR.md
+++ b/README_KR.md
@@ -167,6 +167,7 @@ if __name__ == "__main__":
     - [base_conversion](algorithms/maths/base_conversion.py)
     - [combination](algorithms/maths/combination.py)
     - [extended_gcd](algorithms/maths/extended_gcd.py)
+    - [factorial](algorithms/maths/factorial.py)
     - [gcd/lcm](algorithms/maths/gcd.py)
     - [generate_strobogrammtic](algorithms/maths/generate_strobogrammtic.py)
     - [is_strobogrammatic](algorithms/maths/is_strobogrammatic.py)
diff --git a/algorithms/maths/__init__.py b/algorithms/maths/__init__.py
index d791ad8..2bf44a7 100644
--- a/algorithms/maths/__init__.py
+++ b/algorithms/maths/__init__.py
@@ -1,5 +1,6 @@
 from .base_conversion import *
 from .extended_gcd import *
+from .factorial import *
 from .gcd import *
 from .generate_strobogrammtic import *
 from .is_strobogrammatic import *
diff --git a/algorithms/maths/factorial.py b/algorithms/maths/factorial.py
new file mode 100644
index 0000000..2cd1a34
--- /dev/null
+++ b/algorithms/maths/factorial.py
@@ -0,0 +1,20 @@
+#
+# This function calculates n!
+# Factorial function not works in less than 0
+#
+
+def factorial(n):
+
+    result = 1
+    for i in range(2, n+1):
+        result *= i
+
+    return result
+
+
+def factorial_recur(n):
+    if n == 0:
+        return 1
+    
+    return n * factorial(n-1)
+    


Test patch:
diff --git a/tests/test_maths.py b/tests/test_maths.py
index fbf0772..a5ca21f 100644
--- a/tests/test_maths.py
+++ b/tests/test_maths.py
@@ -1,6 +1,7 @@
 from algorithms.maths import (
     int2base, base2int,
     extended_gcd,
+    factorial, factorial_recur,
     gcd, lcm,
     gen_strobogrammatic, strobogrammatic_in_range,
     is_strobogrammatic, is_strobogrammatic2,
@@ -205,7 +206,7 @@ class TestRSA(unittest.TestCase):
 
 class TestCombination(unittest.TestCase):
     """[summary]
-    Test for the file rsa.py
+    Test for the file combination.py
 
     Arguments:
         unittest {[type]} -- [description]
@@ -215,5 +216,25 @@ class TestCombination(unittest.TestCase):
         self.assertEqual(10, combination(5, 2))
         self.assertEqual(252, combination(10, 5))
         
+class TestFactorial(unittest.TestCase):
+    """[summary]
+    Test for the file factorial.py
+
+    Arguments:
+        unittest {[type]} -- [description]
+    """
+
+    def test_factorial(self):
+        self.assertEqual(1, factorial(0))
+        self.assertEqual(120, factorial(5))
+        self.assertEqual(3628800, factorial(10))
+        
+    def test_factorial_recur(self):
+        self.assertEqual(1, factorial_recur(0))
+        self.assertEqual(120, factorial_recur(5))
+        self.assertEqual(3628800, factorial_recur(10))
+        
 if __name__ == "__main__":
     unittest.main()
+    
+