# 【二项式系数】

# 意义

从N件物件中取出K件的总组合数。

比如 6个取2个的组合有: 12,13,14,15,16,23,24,25,26,34,35,36,45,46,56 这15种组合数。 Markdown 图片

/// <summary>Returns the binomial coefficient n over k</summary>
public static ulong BinomialCoef( uint n, uint k ) {
// source: https://blog.plover.com/math/choose.html
    ulong r = 1;
    if( k > n ) return 0;
    for( ulong d = 1; d <= k; d++ ) {
        r *= n--;
        r /= d;
    }
    return r;
    // mathematically clean but extremely prone to overflow
    //return Factorial( n ) / ( Factorial( k ) * Factorial( n - k ) );
}