One good way to arrange this is
n / 1 * (n-1) / 2 * (n-3) / 3 * ... * (n-r+1) / r
It's easy to see that all the divisions have remainder zero.
> I believe it is possible to keep the results in the range of integers,
> if the final result is.
Let's see: After "times (n-k+1) divided by k" we've calculated
C(n,k). The next product is C(n,k)*(n-k) before dividing by
(k+1) knocks it back down, so it looks like the product could be
somewhat larger than the eventual result, maybe too large. Hmm:
If we try to calculate C(30,15) this way, we'll get as far as
C(30,14) = 145422675
and then multiply by 16
C(30,14)*16 = 2326762800 > Integer.MAX_VALUE
and then divide by 15
C(30,14)*16/15 = C(30,15) = 155117520 < Integer.MAX_VALUE
So although we're much better off than by dividing factorials,
caution is still needed. (This is also a reason to begin by
setting `r = Math.min(r,n-r)': Not only does it make for fewer
iterations, but it helps avoid the central area where the numbers
get big. C(30,2) = C(30,28) mathematically, but 30/1*29/2 won't
get into trouble while 30/1*29/2*...*3/28 will.)
--
Eric Sosman
eso...@comcast-dot-net.invalid