You do say right away that is a valid formula for binomial coefficients, but then you say it's hardly a good one?

It is a formula for calculating r-combinations, not binomial coefficients.

The formula for binomial expansion is:

n
_
(x+y)^n = \ C(n,j) * x^(n-j) *y^j
/
¯
j=0

And btw, it is the definitive formula for calculating r-combinations. It's like saying 4/2 is not the same as 2. While 2 is a better way to write 4/2, not every one recognizes 2 right away, and I feel, for the benefit of the reader of me 'craft', n!/(r!(n-r)! is a better way to go.

As for your function, it is shorter, but a litle mysterious when it comes to the math.

Those formulas are all the same in mathematical terms. But we are talking floating point arithmatic here. Computing the factorials and then dividing is going to introduce errors into the computation for fairly small values of n here.

If you want to understand the math, I fully agree
with your use of the formula. Furthermore for more complex
combinatorial problems, understand how to get those
formulas is extremely important.

That said, tye is right as well. While in theory that
calculation gets the right answer, in practice it starts
to be inaccurate for fairly small numbers. You can get
around this by using a big integer package. (Or a language
that understands integers of arbitrary size.) But that
will be somewhat slow.

If you want to understand this, go with the formula. If
you want to calculate it, go with the tricks. If you want
to calculate answers to arbitrary problems, be prepared to
manipulate big integers and hope that performance doesn't
matter.

And if you want an even better example some day, bug me
about why you should always use row-reduction to find the
inverses of matrices rather than Cramer's formula. (Hint:
Think numerically unstable and exponential running time,
vs n**3 and much better behaved.)

While Perl no longer display integers without using
exponential notation at 18!, when I tried to get a mistake
in the display, I couldn't. Except for the fact that Perl
did not like displaying large numbers, the results are
right.

In retrospect I shouldn't be surprised at this. After all
the internal calculation is carried out at a higher
accuracy than what is displayed. Since the calculation is
pretty short, roundoff errors are not readily visible. And
testing this is pretty simple:

To find a disagreement between theory and practice I had
to go to 28 choose 14. Which was off from being an
integer by about 1 part in a hundred million.

Sorry tye, but I find that error tolerable. For a simple
example the formula works in this case, even though
theoretically in practice it should show some difference
between theory and practice.

OTOH I can guarantee you that trying to invert a 10x10
matrix using Cramer's formula is going to be a better
example. For an nxn matrix you get n! terms being added
and subtracted from each other. It doesn't take a large
n to make that rather slow, and to get insane roundoff
errors...

Perl computes floating-point using doubles, which typically have a 48 bit mantissa. 4-byte ints have just 32 bits of precision, so it's natural that doubles have a larger integer range than ints! So you can pretend Perl has 48-bit integers for most purposes.

It's still not a good idea to compute huge numbers and try to cancel them out. Especially when not doing this takes fewer operations.