Java Binomial Coefficients binomial(int N, int K)

Here you can find the source of binomial(int N, int K)

Description

Compute binomial (N) (K)

License

Open Source License

Parameter

Parameter Description
N >= 0
K >= 0

Return

binomial(N,K)

Declaration

public static long binomial(int N, int K) 

Method Source Code

//package com.java2s;
/*/*from w  ww .j  a v  a 2 s .  co m*/
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Compute binomial (N) (K)
     * 
     * @param N >= 0
     * @param K >= 0
     * @return binomial(N,K)
     */
    public static long binomial(int N, int K) {
        assert N >= 0 : N;
        assert K >= 0 : K;
        long[][] binomial = new long[N + 1][K + 1];

        // base cases
        for (int k = 1; k <= K; k++) {
            binomial[0][k] = 0;
        }
        for (int n = 0; n <= N; n++) {
            binomial[n][0] = 1;
        }

        // bottom-up dynamic programming
        int k;
        for (int n = 1; n <= N; n++) {
            for (k = 1; k <= K; k++) {
                binomial[n][k] = binomial[n - 1][k - 1] + binomial[n - 1][k];
            }
        }

        return binomial[N][K];
    }
}

Related

  1. binomial(final int n, final int k)
  2. binomial(int n, double p)
  3. binomial(int n, final int k)
  4. binomial(int n, int k)
  5. binomialCdf(int k, int n, double p)
  6. binomialCoeff(int n, int k)
  7. binomialCoefficient(double n, double k)