如何计算大数的二项式系数

时间:2012-03-08 15:04:30

标签: c# math binomial-coefficients

我需要在C#中计算n!/(n-r)!r!。对于小数字,使用阶乘函数很容易计算,但是当数字变得像100那样大时,它就不起作用了。有没有其他方法可以计算更大数字的组合?

4 个答案:

答案 0 :(得分:16)

首先,我注意到你正在尝试计算二项式系数,所以我们称之为。

以下是一些进行计算的方法。如果你使用BigInteger,你不必担心溢出:

方法一:使用factorial:

static BigInteger Factorial(BigInteger n)
{
    BigInteger f = 1;
    for (BigInteger i = 2; i <= n; ++i)
        f = f * i;
    return f;
}

static BigInteger BinomialCoefficient(BigInteger n, BigInteger k)
{
    return Factorial(n) / (Factorial(n-k) * Factorial(k));
}

方法二:使用递归:

static BigInteger BinomialCoefficient(BigInteger n, BigInteger k)
{
    if (n == 0) return 1;
    if (k == 0) return 0;
    return BinomialCoefficient(n-1, k-1) + BinomialCoefficient(n-1, k)
}

除非你 memoize 结果,否则这个方法并不快。

方法三:更加巧妙地减少乘法次数,并尽早划分。这使数字变小:

static BigInteger BinomialCoefficient(BigInteger n, BigInteger k)
{
    // (n C k) and (n C (n-k)) are the same, so pick the smaller as k:
    if (k > n - k) k = n - k;
    BigInteger result = 1;
    for (BigInteger i = 1; i <= k; ++i)
    {
        result *= n - k + i;
        result /= i;
    }
    return result;
}

因此,例如,如果你是计算(6 C 3),而不是计算(6 x 5 x 4 x 3 x 2 x 1)/((3 x 2 x 1)x(3 x 2 x 1)) ,你计算(((4/1)* 5)/ 2)* 6)/ 3,如果可能的话,它会使数字变小。

答案 1 :(得分:4)

按照埃里克的说法,尽早划分有很大帮助,你可以通过从高到低来改善它。这样,只要endresult适合Long,您就可以计算任何结果。这是我使用的代码(为Java道歉,但转换应该很容易):

public static long binomialCoefficient(int n, int k) {
   // take the lowest possible k to reduce computing using: n over k = n over (n-k)
   k = java.lang.Math.min( k, n - k );

   // holds the high number: fi. (1000 over 990) holds 991..1000
   long highnumber[] = new long[k];
   for (int i = 0; i < k; i++)
      highnumber[i] = n - i; // the high number first order is important
   // holds the dividers: fi. (1000 over 990) holds 2..10
   int dividers[] = new int[k - 1];
   for (int i = 0; i < k - 1; i++)
      dividers[i] = k - i;

   // for every divider there always exists a highnumber that can be divided by 
   // this, the number of highnumbers being a sequence that equals the number of 
   // dividers. Thus, the only trick needed is to divide in reverse order, so 
   // divide the highest divider first trying it on the highest highnumber first. 
   // That way you do not need to do any tricks with primes.
   for (int divider: dividers) 
      for (int i = 0; i < k; i++)
         if (highnumber[i] % divider == 0) {
            highnumber[i] /= divider;
            break;
         }

   // multiply remainder of highnumbers
   long result = 1;
   for (long high : highnumber)
      result *= high;
   return result;
}

答案 2 :(得分:0)

对于.net 4.0及更大版本,请使用类BigInteger而不是int / long

对于其他.net使用自定义大号类,例如来自IntX:http://www.codeplex.com/IntX/

答案 3 :(得分:0)

我认为这将是有效的 它是O(k)

注意n! / r! r!只是取消n的最后一个r 所以7 3

7 x 6 x 5 x 4 x 3 x 2 x 1 
over 
            4 x 3 x 2 x 1 

public static uint BinomialCoeffient(uint n, uint k)
{
    if (k > n)
        return 0;

    uint c = n;
    for (uint i = 1; i < k; i++)
    {
        c *= n - i;
        c /= i + 1;
    }
    return c;
}