Java Binomial Coefficients binomialCoefficient(int n, int k)

Here you can find the source of binomialCoefficient(int n, int k)

Description

binomial Coefficient

License

Open Source License

Declaration

public static final long binomialCoefficient(int n, int k) 

Method Source Code

//package com.java2s;
/*//  w ww.ja v  a 2  s  .  co  m
 * Copyright (C) 2008-2015 by Holger Arndt
 *
 * This file is part of the Universal Java Matrix Package (UJMP).
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership and licensing.
 *
 * UJMP is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * UJMP 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with UJMP; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

public class Main {
    public static final long binomialCoefficient(int n, int k) {
        if (k > n || k < 0) {
            return 0;
        } else {
            return factorial(n) / factorial(k) / factorial(n - k);
        }
    }

    public static final long factorial(int n) {
        long r = 1;
        for (int i = 2; i <= n; i++) {
            r *= i;
        }
        return r;
    }
}

Related

  1. binomialCdf(int k, int n, double p)
  2. binomialCoeff(int n, int k)
  3. binomialCoefficient(double n, double k)
  4. binomialCoefficient(int n, int k)
  5. binomialCoefficient(int n, int k)
  6. binomialCoefficient(long n, long k)
  7. binomialCoefficientDouble(final int n, final int k)
  8. binomialCoefficientLog(final int n, final int k)
  9. binomialCoefficients(int n, int k)