From 79c7e38bb6fe6ffdd99ddd81d0ccc9b1957be283 Mon Sep 17 00:00:00 2001 From: SubhranShu2332 <124662904+SubhranShu2332@users.noreply.github.com> Date: Sat, 7 Oct 2023 21:53:11 +0530 Subject: [PATCH 1/4] In place of calculating the factorial several times we can run a loop k times to calculate the combination for example: 5 C 3 = 5! / (3! * (5-3)! ) = (5*4*3*2*1)/[(3*2*1)*(2*1)] =(5*4*3)/(3*2*1) so running a loop k times will reduce the time complexity to O(k) --- maths/combinations.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/maths/combinations.py b/maths/combinations.py index a2324012c01f..4cb24c0eb418 100644 --- a/maths/combinations.py +++ b/maths/combinations.py @@ -1,8 +1,6 @@ """ https://en.wikipedia.org/wiki/Combination """ -from math import factorial - def combinations(n: int, k: int) -> int: """ @@ -35,7 +33,12 @@ def combinations(n: int, k: int) -> int: # to calculate a factorial of a negative number, which is not possible if n < k or k < 0: raise ValueError("Please enter positive integers for n and k where n >= k") - return factorial(n) // (factorial(k) * factorial(n - k)) + res=1 + for i in range(k): + res = res * (n - i) + res = res // (i + 1) + return res + if __name__ == "__main__": From ec2491c8835338474acba43d7e4217ec90cc6c71 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Oct 2023 19:51:38 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/combinations.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/combinations.py b/maths/combinations.py index 4cb24c0eb418..f65155801b52 100644 --- a/maths/combinations.py +++ b/maths/combinations.py @@ -2,6 +2,7 @@ https://en.wikipedia.org/wiki/Combination """ + def combinations(n: int, k: int) -> int: """ Returns the number of different combinations of k length which can @@ -33,12 +34,11 @@ def combinations(n: int, k: int) -> int: # to calculate a factorial of a negative number, which is not possible if n < k or k < 0: raise ValueError("Please enter positive integers for n and k where n >= k") - res=1 + res = 1 for i in range(k): res = res * (n - i) res = res // (i + 1) return res - if __name__ == "__main__": From a91ff20bd53e26928ae972037ce9a3c245f8e449 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Sun, 8 Oct 2023 15:12:50 -0400 Subject: [PATCH 3/4] Update maths/combinations.py --- maths/combinations.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/combinations.py b/maths/combinations.py index f65155801b52..bf9aa6b7ef90 100644 --- a/maths/combinations.py +++ b/maths/combinations.py @@ -36,8 +36,8 @@ def combinations(n: int, k: int) -> int: raise ValueError("Please enter positive integers for n and k where n >= k") res = 1 for i in range(k): - res = res * (n - i) - res = res // (i + 1) + res *= (n - i) + res //= (i + 1) return res From 62ba0593297ce4755c86272f0660b9187c9e4e61 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 8 Oct 2023 19:13:16 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/combinations.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/combinations.py b/maths/combinations.py index bf9aa6b7ef90..6e9e1a807067 100644 --- a/maths/combinations.py +++ b/maths/combinations.py @@ -36,8 +36,8 @@ def combinations(n: int, k: int) -> int: raise ValueError("Please enter positive integers for n and k where n >= k") res = 1 for i in range(k): - res *= (n - i) - res //= (i + 1) + res *= n - i + res //= i + 1 return res