Compute binomial coefficient or combinations with Dynamic Programming

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.