Returns a factored form for very large binomial coefficients
Updated 16 Feb 2010

View License

Every once in a while someone wishes to compute a VERY large binomial coefficient. While I'll admit that most of the time the proper solution is to compute the log of the coefficient. The best solution for this is to use gammaln.

n = 20000000;
k = 5000000;
gammaln(n+1) - gammaln(k+1) - gammaln(n-k+1)
ans =

Once in a rare while someone might wish to compute the integer factors of this coefficient. I've now had two recent occasions to look for such a factorization, so I assume there must be someone else in the world who has a use for them too.

tic,[facs,count] = binomfactors(20000000,5000000);toc
Elapsed time is 43.071204 seconds.

We can verify the result by computing the log of the binomial coefficient from the factors. See that it is identical to that returned from gammaln above.

ans =

How many digits are in this number?

ans =

I did say it was large, with approximately 4.8 million digits.

So, for that rare person who wishes to compute the factors of a binomial coefficient, binomfactors does exactly that. Or, if all you wish to see are some ideas on how one might design a prime sieve for such a computation in MATLAB, this might be of some interest.

(Note that while this tool is used in my vpi toolbox, it does not require vpi to run.)

Cite As

John D'Errico (2024). binomfactors (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2007b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes