The Problem
Write a function that takes two parameters N and K and returns the value of binomial coefficient C (n, k ).For example, your function shocould return 6 for n = 4 and K = 2, and it shoshould return 10 for n = 5 and K = 2.
1 Def Computebinomialcoefficients (n, k ): 2 # Computebinomialcoefficients (5, 2) shoud return 10 3 Table = [0] * (k + 1) 4 Table [0] = 1 5 For I In Range (1, n + 1 ): 6 For J In Range (min (I, K), 0,-1 ): # Why begin with Min (I, K): 1) We do not need any results more than K. 2) Yang Hui (Pascal) triangle: J must be less than I 7 Table [J] = table [J] + Table [J-1]# This comes from Yang Hui triangle (or Pascal trianle) of Binomial Coefficients 8 9 Return Table [k]
Referece:
Http://www.geeksforgeeks.org/dynamic-programming-set-9-binomial-coefficient/
Http://en.wikipedia.org/wiki/Pascal%27s_triangle
Compute binomial coefficient or combinations with Dynamic Programming