Binomial coefficient - a lamer's question
Hi all,
I'm quite new to programming, and trying to write a c++ program that calculates the binomial coefficient of two numbers. This can be used for calculating the probability of, for example, winning a lottery game. The formula is as follows:
Code:
n!
--------
(n-k)!k!
Where ! means the factorial of the number. My problem is that if I want to calculate, say, the number of possible combinations in the state lottery game in my country where you can select 5 numbers out of 90, the program has to deal with VERY large numbers, like 90! which is 1 x 2 x ... x 89 x 90. My idea was that n! is divisible by (n-k)!, so our example then looks like:
Code:
90! 1 x 2 x ... x 85 x ... x 90 86 x 87 x 88 x 90
--------- = --------------------------- = -----------------
(90-5)!5! 1 x 2 x ... x 85 x 1 x...x5 1 x 2 x ... x 5
So I've written a function that returns the product of a number series, like 86-90. It works with smaller numbers but it seems that 86 x ... x 90 is too large for it, even if I set the data type to unsigned long int. The function is:
Code:
unsigned long int multiply_from_to (int From, int To) {
int i, Product=1;
for (i=0;i<=To-From;i++) {
Product*=From+i;
}
return Product;
}
What's wrong? (and sorry for the long post)