Recursive

Binomial Coefficient

Pinterest LinkedIn Tumblr

Binomial coefficients are a family of positive integers that occur as coefficients in the binomial theorem. The binomial coefficients can be calculated recursively.

Write a program to implement the binomial coefficients.

source Code

package com.dsacode.Algorithm.recursive;
 
public class BinomialCoefficient {
     private static long binomial(int n, int k)
        {
            if (k>n-k)
                k=n-k;
            long b=1;
            for (int i=1, m=n; i <= k; i++, m--)
                b=b*m/i;
            return b;
        }
    public static void main(String[] args) {
         System.out.println("binomial of '5' and '3': "+binomial(5, 3));
 
    }
 
}

Output

Binomial of '5' and '3': 10

Algorithm Explanation

Get the number.
A binomial coefficient equals the number of combinations of r items that can be selected from a set of n items -1 recursively.
Print the value

Reference

  1. http://www.mathwords.com/b/binomial_coefficients.htm
  2. https://en.wikipedia.org/wiki/Binomial_coefficient
  3. http://www.ohrt.com/odds/binomial.php
  4. http://mathworld.wolfram.com/BinomialCoefficient.html

Write A Comment