File Exchange

image thumbnail


version (394 Bytes) by David Terr
This program computes the binomial coefficient C(n,m).

1 Download

Updated 04 Jun 2004

No License

Given nonnegative integers m and n with m <= n, this program computes the binomial coefficient C(n,m). It's about time someone wrote code to do this! I couldn't find it anywhere.

Cite As

David Terr (2021). binomial.m (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (5)

John D'Errico

Sorry. I think Herbert completely misunderstands the issue. In fact, he seems to be wrong on all counts.

It DOES have an overflow issue, just as as nchoosek has an issue. For example,

>> nchoosek(1500,265)
Warning: Result may not be exact. Coefficient is greater than 9.007199e+15 and is only accurate to 15 digits
> In nchoosek at 92
ans =

nchoosek does return a valid answer, WITH a warning that it is not exact. But since the result has 303 decimal digits and is returned as a double what do you expect???

binomial also returns the same thing, however it generates no warning message. That does not say it has no issue, only that it fails to be friendly and warn you of that fact!

Both codes will return inf when the arguments cause a double to be exceeded.

ans =

However, note that nchoosek is both more flexible and more powerful than is binomial. And nchoosek has been around since the dark ages. So the fact that David could not find it is merely a reflection that he did not look that hard. Jos points out the simple use of lookfor that would have given him the answer.

nchoosek also has the ability to give exact results for a slightly larger set of inputs than binomial. So if the input is uint64, so will be the output.

>> b1 = nchoosek(uint64(65),30)
b1 =

>> b2 = binomial(65,30)
b2 =

>> b1 - b2
ans =

Here nchoosek was exact, whereas binomial was wrong. In fact, nchoosek CAN do symbolic computation, whereas binomial fails to return anything of value.

>> b1 = nchoosek(sym(1500),750)
b1 =

>> binomial(sym(1500),30)
ans =
exp(gammaln(1501) - gammaln(1471) - 5253606334386405/70368744177664)

Of course, nchoosek even works perfectly for vpi input, whereas binomial fails there completely.

There is only one point made by Herbert that MIGHT be correct in some circumstances, i.e., that binomial is faster. Since binomial does no error checking and it has lesser capability than does nchoosek, of course it might be faster. On top of that, the gamma log computation for large input might be be pretty fast. But is it good? Even for double inputs...

>> binomial(101,10)
ans =

>> nchoosek(101,10)
ans =

Oops. I'm pretty sure that most binomial coefficients are integers, at least they were when I went to school. You never know with the new math though. But I would think it disappointing to see a floating point result that is not a pure integer, at least for a reasonably small set of inputs.

So how about speed. Is it faster? I'll use timeit to do the comparison.

>> timeit(@() nchoosek(100,10))
ans =

>> timeit(@() binomial(100,10))
ans =

So here nchoosek was actually 4 times FASTER than binomial. Not slower. binomial is the slow one.


perfect, it's faster than nchoosek and doesn't seem to have an overflow issue.

Mark Pijnenburg

Indeed this function does not work for symbolic computations so should be removed from this list. It works for numerical computations but for this there are better alternatives, e.g. nchoosek or other.

B Hill

nchoosek works fine. This function only appears to support numeric calculation. Remove post from "Symoblic Math" section as it does not belong here.

Jos vdG

lookfor "binomial coefficient" returns

MATLAB Release Compatibility
Created with R12
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!