djfr33 wrote: |
---|
Jonnin it is supposed to be working off of doubles. |
There is absolutely NO need to use doubles. 18C3 is only 816 - well within integer arithmetic provided that you DON'T use a factorial function.
Nor is there any need whatsoever to use a factorial function at all.
Think what 18!/3!15! actually cancels down to:
(18*17*16)/(3*2*1)
No real need to compute a factorial there (not even in the denominator).
Rearrange it as (and compute in the following order to prevent truncation and keep intermediate results small):
16 / 1 * 17 / 2 * 18 / 3
In general, for nCr, replace r by the smaller of r and n-r (to save computations). Then compute:
(n-r+1) / 1 * (n - r + 2 ) / 2 * ..... n / r
No factorials. All integers.
To push it further, if you work in, and return, unsigned long long, then you should be able to get up to about n=62 before any of the binomial coefficients start to misbehave. At this point, doubles will be very wrong.
Another option - with no multiplies - is to use Pascal's triangle.
BTW:
If there are 18 people in your class and you want to divide the class into programming teams of 3 |
Sorry, but nCr (or n!/(n-r)!r!) gives you the number of ways of selecting ONE team of 3 from 18 people ... it's not really what the original question asked.