2019-10-26 09:18:28 +00:00
|
|
|
def binomial_coefficient(n, r):
|
|
|
|
"""
|
|
|
|
Find binomial coefficient using pascals triangle.
|
|
|
|
|
|
|
|
>>> binomial_coefficient(10, 5)
|
|
|
|
252
|
|
|
|
"""
|
2022-10-12 22:54:20 +00:00
|
|
|
c = [0 for i in range(r + 1)]
|
2019-10-26 09:18:28 +00:00
|
|
|
# nc0 = 1
|
2022-10-12 22:54:20 +00:00
|
|
|
c[0] = 1
|
2019-10-26 09:18:28 +00:00
|
|
|
for i in range(1, n + 1):
|
|
|
|
# to compute current row from previous row.
|
|
|
|
j = min(i, r)
|
|
|
|
while j > 0:
|
2022-10-12 22:54:20 +00:00
|
|
|
c[j] += c[j - 1]
|
2019-10-26 09:18:28 +00:00
|
|
|
j -= 1
|
2022-10-12 22:54:20 +00:00
|
|
|
return c[r]
|
2019-10-26 09:18:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
print(binomial_coefficient(n=10, r=5))
|