From a57809af9c7611ae119b44b3279fee6f7ce7049c Mon Sep 17 00:00:00 2001 From: prathmesh1199 <51616294+prathmesh1199@users.noreply.github.com> Date: Sat, 26 Oct 2019 14:48:28 +0530 Subject: [PATCH] Added binomial coefficient (#1467) * Added binomial coefficient * Format code with psf/black and add a doctest --- maths/binomial_coefficient.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 maths/binomial_coefficient.py diff --git a/maths/binomial_coefficient.py b/maths/binomial_coefficient.py new file mode 100644 index 000000000..4def04149 --- /dev/null +++ b/maths/binomial_coefficient.py @@ -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))