From 4f457c5786dab2e83f00d671e7721528ed385eb9 Mon Sep 17 00:00:00 2001 From: Naman-Vasudev Date: Mon, 20 Oct 2025 15:07:03 +0530 Subject: [PATCH 1/5] Create ncr_combinations.py --- maths/ncr_combinations.py | 74 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 maths/ncr_combinations.py diff --git a/maths/ncr_combinations.py b/maths/ncr_combinations.py new file mode 100644 index 000000000000..ebe17735a32c --- /dev/null +++ b/maths/ncr_combinations.py @@ -0,0 +1,74 @@ +""" +Generalized nCr (combinations) calculator for real numbers n and integer r. +Wikipedia URL: https://en.wikipedia.org/wiki/Binomial_coefficient +""" + +from math import factorial as math_factorial + + +def nCr(n: float, r: int) -> float: + """ + Compute the number of combinations (n choose r) for real n and integer r + using the formula: + + nCr = n * (n-1) * (n-2) * ... * (n-r+1) / r! + + Parameters + ---------- + n : float + Total number of items. Can be any real number. + r : int + Number of items to choose. Must be a non-negative integer. + + Returns + ------- + float + The number of combinations. + + Raises + ------ + ValueError + If r is not an integer or r < 0 + + Examples + -------- + >>> nCr(5, 2) + 10.0 + >>> nCr(5.5, 2) + 12.375 + >>> nCr(10, 0) + 1.0 + >>> nCr(0, 0) + 1.0 + >>> nCr(5, -1) + Traceback (most recent call last): + ... + ValueError: r must be a non-negative integer + >>> nCr(5, 2.5) + Traceback (most recent call last): + ... + ValueError: r must be a non-negative integer + """ + if not isinstance(r, int) or r < 0: + raise ValueError("r must be a non-negative integer") + + if r == 0: + return 1.0 + + numerator = 1.0 + for i in range(r): + numerator *= n - i + + denominator = math_factorial(r) + return numerator / denominator + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + + # Example usage + n = float(input("Enter n (real number): ").strip() or 0) + r = int(input("Enter r (integer): ").strip() or 0) + print(f"nCr({n}, {r}) = {nCr(n, r)}") From 725abb4a59aebc91e196901399b91a87e029ac8f Mon Sep 17 00:00:00 2001 From: Naman-Vasudev Date: Mon, 20 Oct 2025 15:09:32 +0530 Subject: [PATCH 2/5] Update ncr_combinations.py --- maths/ncr_combinations.py | 50 +++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/maths/ncr_combinations.py b/maths/ncr_combinations.py index ebe17735a32c..ca24b44afcb7 100644 --- a/maths/ncr_combinations.py +++ b/maths/ncr_combinations.py @@ -1,24 +1,24 @@ """ -Generalized nCr (combinations) calculator for real numbers n and integer r. +Generalized combinations (n choose r) calculator for real total and integer choose. Wikipedia URL: https://en.wikipedia.org/wiki/Binomial_coefficient """ from math import factorial as math_factorial -def nCr(n: float, r: int) -> float: +def combinations(total: float, choose: int) -> float: """ - Compute the number of combinations (n choose r) for real n and integer r + Compute the number of combinations (total choose choose) for real total and integer choose using the formula: - nCr = n * (n-1) * (n-2) * ... * (n-r+1) / r! + combinations = total * (total-1) * ... * (total-choose+1) / choose! Parameters ---------- - n : float + total : float Total number of items. Can be any real number. - r : int - Number of items to choose. Must be a non-negative integer. + choose : int + Number of items to select. Must be a non-negative integer. Returns ------- @@ -28,38 +28,38 @@ def nCr(n: float, r: int) -> float: Raises ------ ValueError - If r is not an integer or r < 0 + If choose is not a non-negative integer. Examples -------- - >>> nCr(5, 2) + >>> combinations(5, 2) 10.0 - >>> nCr(5.5, 2) + >>> combinations(5.5, 2) 12.375 - >>> nCr(10, 0) + >>> combinations(10, 0) 1.0 - >>> nCr(0, 0) + >>> combinations(0, 0) 1.0 - >>> nCr(5, -1) + >>> combinations(5, -1) Traceback (most recent call last): ... - ValueError: r must be a non-negative integer - >>> nCr(5, 2.5) + ValueError: choose must be a non-negative integer + >>> combinations(5, 2.5) Traceback (most recent call last): ... - ValueError: r must be a non-negative integer + ValueError: choose must be a non-negative integer """ - if not isinstance(r, int) or r < 0: - raise ValueError("r must be a non-negative integer") + if not isinstance(choose, int) or choose < 0: + raise ValueError("choose must be a non-negative integer") - if r == 0: + if choose == 0: return 1.0 numerator = 1.0 - for i in range(r): - numerator *= n - i + for i in range(choose): + numerator *= total - i - denominator = math_factorial(r) + denominator = math_factorial(choose) return numerator / denominator @@ -69,6 +69,6 @@ def nCr(n: float, r: int) -> float: doctest.testmod() # Example usage - n = float(input("Enter n (real number): ").strip() or 0) - r = int(input("Enter r (integer): ").strip() or 0) - print(f"nCr({n}, {r}) = {nCr(n, r)}") + total_input = float(input("Enter total (real number): ").strip() or 0) + choose_input = int(input("Enter choose (integer): ").strip() or 0) + print(f"combinations({total_input}, {choose_input}) = {combinations(total_input, choose_input)}") From ab5dfe38193f7376eaad5dfa4eba35e3a8817f1e Mon Sep 17 00:00:00 2001 From: Naman-Vasudev Date: Mon, 20 Oct 2025 15:10:34 +0530 Subject: [PATCH 3/5] Update ncr_combinations.py --- maths/ncr_combinations.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/maths/ncr_combinations.py b/maths/ncr_combinations.py index ca24b44afcb7..5fded68c8f2d 100644 --- a/maths/ncr_combinations.py +++ b/maths/ncr_combinations.py @@ -1,6 +1,7 @@ """ Generalized combinations (n choose r) calculator for real total and integer choose. -Wikipedia URL: https://en.wikipedia.org/wiki/Binomial_coefficient +Wikipedia URL: +https://en.wikipedia.org/wiki/Binomial_coefficient """ from math import factorial as math_factorial @@ -70,5 +71,8 @@ def combinations(total: float, choose: int) -> float: # Example usage total_input = float(input("Enter total (real number): ").strip() or 0) - choose_input = int(input("Enter choose (integer): ").strip() or 0) - print(f"combinations({total_input}, {choose_input}) = {combinations(total_input, choose_input)}") + choose_input = int(input("Enter choose (non-negative integer): ").strip() or 0) + print( + f"combinations({total_input}, {choose_input}) = " + f"{combinations(total_input, choose_input)}" + ) From c15888afbdc4d5ab4a30c1433246817a206983e2 Mon Sep 17 00:00:00 2001 From: Naman-Vasudev Date: Mon, 20 Oct 2025 15:11:48 +0530 Subject: [PATCH 4/5] Update ncr_combinations.py --- maths/ncr_combinations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/ncr_combinations.py b/maths/ncr_combinations.py index 5fded68c8f2d..7b7a0d37ea9a 100644 --- a/maths/ncr_combinations.py +++ b/maths/ncr_combinations.py @@ -9,7 +9,7 @@ def combinations(total: float, choose: int) -> float: """ - Compute the number of combinations (total choose choose) for real total and integer choose + Compute the number of combinations using the formula: combinations = total * (total-1) * ... * (total-choose+1) / choose! From 16cab8cb89f928cc165cfee7ee60e6861cd717b2 Mon Sep 17 00:00:00 2001 From: Naman-Vasudev Date: Mon, 20 Oct 2025 15:13:47 +0530 Subject: [PATCH 5/5] Update ncr_combinations.py --- maths/ncr_combinations.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/maths/ncr_combinations.py b/maths/ncr_combinations.py index 7b7a0d37ea9a..49add3259804 100644 --- a/maths/ncr_combinations.py +++ b/maths/ncr_combinations.py @@ -9,8 +9,7 @@ def combinations(total: float, choose: int) -> float: """ - Compute the number of combinations - using the formula: + Compute the number of combinations using the formula: combinations = total * (total-1) * ... * (total-choose+1) / choose!