码迷,mamicode.com
首页 > 其他好文 > 详细

compute Binomial Coefficient or combinations with dynamic programming

时间:2014-08-23 12:41:20      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   strong   for   ar   div   

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 should return 6 for n = 4 and k = 2, and it should 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

bubuko.com,布布扣

compute Binomial Coefficient or combinations with dynamic programming

标签:style   blog   http   color   io   strong   for   ar   div   

原文地址:http://www.cnblogs.com/asrman/p/3930887.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!