Improved Combinations with the BigInteger Data Type: Listing 3.

Successor method to return next combination.

public Combination Successor()
{
  if (this.data.Length == 0 ||
    this.data[0] == this.n - this.k)
    return null;

  Combination result = new Combination(this.n, this.k);

  int i;
  for (i = 0; i < this.k; ++i)
    result.data[i] = this.data[i];

  i = this.k - 1;
  while (i > 0 && result.data[i] == this.n - this.k + i)
    --i;

  ++result.data[i];

  for (int j = i; j < this.k - 1; ++j)
    result.data[j + 1] = result.data[j] + 1;

  return result;
}

About the Author

Dr. James McCaffrey works for Microsoft Research in Redmond, WA. James has worked on several key Microsoft products such as Internet Explorer and Bing. James can be reached at jamccaff@microsoft.com.