Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.

Sunday, May 5, 2013

Single loop: Binomial Coefficient


Problem: Compute binomial coefficient

C(r,k) = r(r-1)...(r-k+1) / (k(k-1)...(1))

Solution in Python:


from __future__ import division

def binomial_coefficient(r, k):

    """
    Author: Mayur P Srivastava
    """

    p = 1
    for j in range(1, k+1):
        p *= (r+1-j) / j
    return p


Concepts learned: Single loop and binomial coefficient

No comments:

Post a Comment