mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-30 16:31:08 +00:00
Added binomial coefficient (#1467)
* Added binomial coefficient * Format code with psf/black and add a doctest
This commit is contained in:
parent
a61b05b10c
commit
a57809af9c
20
maths/binomial_coefficient.py
Normal file
20
maths/binomial_coefficient.py
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
def binomial_coefficient(n, r):
|
||||||
|
"""
|
||||||
|
Find binomial coefficient using pascals triangle.
|
||||||
|
|
||||||
|
>>> binomial_coefficient(10, 5)
|
||||||
|
252
|
||||||
|
"""
|
||||||
|
C = [0 for i in range(r + 1)]
|
||||||
|
# nc0 = 1
|
||||||
|
C[0] = 1
|
||||||
|
for i in range(1, n + 1):
|
||||||
|
# to compute current row from previous row.
|
||||||
|
j = min(i, r)
|
||||||
|
while j > 0:
|
||||||
|
C[j] += C[j - 1]
|
||||||
|
j -= 1
|
||||||
|
return C[r]
|
||||||
|
|
||||||
|
|
||||||
|
print(binomial_coefficient(n=10, r=5))
|
Loading…
Reference in New Issue
Block a user