Here you can find the source of binomialCoefficient(long n, long k)
Binomial coefficient, also known as "n choose k".
Parameter | Description |
---|---|
n | Total number of samples. n > 0 |
k | Number of elements to choose. <code>n >= k</code>, <code>k >= 0</code> |
public static long binomialCoefficient(long n, long k)
//package com.java2s; /*//w w w . j a v a2 s . c om This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures Copyright (C) 2015 Ludwig-Maximilians-Universit?t M?nchen Lehr- und Forschungseinheit f?r Datenbanksysteme ELKI Development Team This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero 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 Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ public class Main { /** * <p> * Binomial coefficient, also known as "n choose k". * </p> * * @param n Total number of samples. n > 0 * @param k Number of elements to choose. <code>n >= k</code>, * <code>k >= 0</code> * @return n! / (k! * (n-k)!) */ public static long binomialCoefficient(long n, long k) { final long m = Math.max(k, n - k); double temp = 1; for (long i = n, j = 1; i > m; i--, j++) { temp = temp * i / j; } return (long) temp; } /** * Binary max, <i>without</i> handling of special values. * * Because of the lack of special case handling, this is faster than * {@link Math#max}. But usually, it should be written inline as * {@code (a >= b) ? a : b} * * The result is asymmetric in case of {@code Double.NaN}:<br /> * {@code MathUtil.max(Double.NaN, 1.)} is 1, but <br /> * {@code MathUtil.max(1., Double.NaN)} is {@code Double.NaN}. * * @param a First value * @param b Second value * @return Maximum */ public static double max(double a, double b) { return a >= b ? a : b; } /** * Ternary max, <i>without</i> handling of special values. * * Because of the lack of special case handling, this is faster than * {@link Math#max}. But usually, it should be written inline. * * @param a First value * @param b Second value * @param c Third value * @return Maximum */ public static double max(double a, double b, double c) { return a >= b ? (a >= c ? a : c) : (b >= c ? b : c); } /** * Quadrary max, <i>without</i> handling of special values. * * Because of the lack of special case handling, this is faster than * {@link Math#max}. But usually, it should be written inline. * * @param a First value * @param b Second value * @param c Third value * @param d Fourth value * @return Maximum */ public static double max(double a, double b, double c, double d) { return a >= b ? // a >= c ? (a >= d ? a : d) : (c >= d ? c : d) // : // b >= c ? (b >= d ? b : d) : (c >= d ? c : d); } /** * Binary max. If possible, inline. * * @param a First value * @param b Second value * @return Maximum */ public static int max(int a, int b) { return a >= b ? a : b; } /** * Ternary max. If possible, inline. * * @param a First value * @param b Second value * @param c Third value * @return Maximum */ public static int max(int a, int b, int c) { return a >= b ? (a >= c ? a : c) : (b >= c ? b : c); } /** * Quadrary max, <i>without</i> handling of special values. * * Because of the lack of special case handling, this is faster than * {@link Math#max}. But usually, it should be written inline. * * @param a First value * @param b Second value * @param c Third value * @param d Fourth value * @return Maximum */ public static int max(int a, int b, int c, int d) { return a >= b ? // a >= c ? (a >= d ? a : d) : (c >= d ? c : d) // : // b >= c ? (b >= d ? b : d) : (c >= d ? c : d); } }