Improved Combinations with the BigInteger Data Type: Listing 4.

Improved mth lexicographical element of a combination.

public Combination Element(BigInteger m)
{
  BigInteger maxM = Choose(this.n, this.k) - 1;

  if (m > maxM)
    throw new Exception("m value is too large in Element");

  int[] ans = new int[this.k];

  int a = this.n;
  int b = this.k;
  BigInteger x = maxM - m; // x is the "dual" of m

  for (int i = 0; i < this.k; ++i)
  {
    ans[i] = LargestV(a, b, x); // see helper below    
    x = x - Choose(ans[i], b);
    a = ans[i];
    b = b - 1;
  }

  for (int i = 0; i < this.k; ++i)
    ans[i] = (n - 1) - ans[i];

  Combination result = new Combination(this.n, this.k);
  ans.CopyTo(result.data, 0);
  return result;
}

About the Author

Dr. James McCaffrey directs the data science and research efforts at Quaetrix, a data analytics company located near Redmond, Washington. Before joining Quaetrix, James was a senior research engineer at Microsoft. James can be reached at [email protected].

comments powered by Disqus

Featured

  • Spring AI 2.0 Goes GA, Giving Java Developers a More Mature AI App Stack

    Spring AI 2.0 advances the Java framework for generative AI apps with a Spring Boot 4 baseline, cleaner agentic tooling, Model Context Protocol support and vendor-backed integrations including Azure Cosmos DB.

  • Kubernetes for Developers

    Microsoft's Dan Wahlin previews his introductory "Kubernetes for Developers" session at Visual Studio Live! San Diego 2026, explaining how developers can get past the Kubernetes learning curve by starting locally, mastering Pods first, and using Services to make containerized applications reliably accessible.

  • VS Code Keeps Eye on Costs in v1.126 Update

    Visual Studio Code 1.126 adds session-level Copilot cost information, continuing Microsoft's recent focus on helping developers monitor and manage usage-based GitHub Copilot billing.

  • Open VSX 1.0.0 Puts Focus on Open Extension Registry for VS Code Ecosystem

    Eclipse Open VSX has reached 1.0.0, highlighting its role as a vendor-neutral registry for VS Code-compatible extensions.

Subscribe on YouTube