Skip to content

Commit 5c8e8a2

Browse files
fix(maths): correct binomial_coefficient when n is 0 and r > 0
1 parent 6e03a48 commit 5c8e8a2

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

‎maths/binomial_coefficient.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ def binomial_coefficient(n: int, r: int) -> int:
1212
252
1313
>>> binomial_coefficient(10, 0)
1414
1
15-
>>> binomial_coefficient(0, 10)
15+
>>> binomial_coefficient(0, 0)
1616
1
17+
>>> binomial_coefficient(0, 10)
18+
0
1719
>>> binomial_coefficient(10, 10)
1820
1
1921
>>> binomial_coefficient(5, 2)
@@ -41,8 +43,10 @@ def binomial_coefficient(n: int, r: int) -> int:
4143
"""
4244
if n < 0 or r < 0:
4345
raise ValueError("n and r must be non-negative integers")
44-
if 0 in (n, r):
46+
if r == 0:
4547
return 1
48+
if r > n:
49+
return 0
4650
c = [0 for i in range(r + 1)]
4751
# nc0 = 1
4852
c[0] = 1

0 commit comments

Comments
 (0)