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

Return

the binomial coefficient C(n, k)

Declaration

public static long binomialCoefficient(int n, int k) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 University of Illinois at Urbana-Champaign and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://  w  w  w . j  a v  a  2  s .  c o m
 *    UIUC - Initial API and implementation
 *******************************************************************************/

public class Main {
    /** @return the binomial coefficient <i>C(n, k)</i> */
    public static long binomialCoefficient(int n, int k) {
        if (k > n)
            return 0;

        long result = 1;
        for (int i = 1; i <= k; i++)
            result = result * (n - k + i) / i;
        return result;
    }
}

Related

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