Skip to content

Commit 725abb4

Browse files
committed
Update ncr_combinations.py
1 parent 4f457c5 commit 725abb4

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

maths/ncr_combinations.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
"""
2-
Generalized nCr (combinations) calculator for real numbers n and integer r.
2+
Generalized combinations (n choose r) calculator for real total and integer choose.
33
Wikipedia URL: https://en.wikipedia.org/wiki/Binomial_coefficient
44
"""
55

66
from math import factorial as math_factorial
77

88

9-
def nCr(n: float, r: int) -> float:
9+
def combinations(total: float, choose: int) -> float:
1010
"""
11-
Compute the number of combinations (n choose r) for real n and integer r
11+
Compute the number of combinations (total choose choose) for real total and integer choose
1212
using the formula:
1313

14-
nCr = n * (n-1) * (n-2) * ... * (n-r+1) / r!
14+
combinations = total * (total-1) * ... * (total-choose+1) / choose!
1515

1616
Parameters
1717
----------
18-
n : float
18+
total : float
1919
Total number of items. Can be any real number.
20-
r : int
21-
Number of items to choose. Must be a non-negative integer.
20+
choose : int
21+
Number of items to select. Must be a non-negative integer.
2222

2323
Returns
2424
-------
@@ -28,38 +28,38 @@ def nCr(n: float, r: int) -> float:
2828
Raises
2929
------
3030
ValueError
31-
If r is not an integer or r < 0
31+
If choose is not a non-negative integer.
3232

3333
Examples
3434
--------
35-
>>> nCr(5, 2)
35+
>>> combinations(5, 2)
3636
10.0
37-
>>> nCr(5.5, 2)
37+
>>> combinations(5.5, 2)
3838
12.375
39-
>>> nCr(10, 0)
39+
>>> combinations(10, 0)
4040
1.0
41-
>>> nCr(0, 0)
41+
>>> combinations(0, 0)
4242
1.0
43-
>>> nCr(5, -1)
43+
>>> combinations(5, -1)
4444
Traceback (most recent call last):
4545
...
46-
ValueError: r must be a non-negative integer
47-
>>> nCr(5, 2.5)
46+
ValueError: choose must be a non-negative integer
47+
>>> combinations(5, 2.5)
4848
Traceback (most recent call last):
4949
...
50-
ValueError: r must be a non-negative integer
50+
ValueError: choose must be a non-negative integer
5151
"""
52-
if not isinstance(r, int) or r < 0:
53-
raise ValueError("r must be a non-negative integer")
52+
if not isinstance(choose, int) or choose < 0:
53+
raise ValueError("choose must be a non-negative integer")
5454

55-
if r == 0:
55+
if choose == 0:
5656
return 1.0
5757

5858
numerator = 1.0
59-
for i in range(r):
60-
numerator *= n - i
59+
for i in range(choose):
60+
numerator *= total - i
6161

62-
denominator = math_factorial(r)
62+
denominator = math_factorial(choose)
6363
return numerator / denominator
6464

6565

@@ -69,6 +69,6 @@ def nCr(n: float, r: int) -> float:
6969
doctest.testmod()
7070

7171
# Example usage
72-
n = float(input("Enter n (real number): ").strip() or 0)
73-
r = int(input("Enter r (integer): ").strip() or 0)
74-
print(f"nCr({n}, {r}) = {nCr(n, r)}")
72+
total_input = float(input("Enter total (real number): ").strip() or 0)
73+
choose_input = int(input("Enter choose (integer): ").strip() or 0)
74+
print(f"combinations({total_input}, {choose_input}) = {combinations(total_input, choose_input)}")

0 commit comments

Comments
 (0)