So, the factorial of a number is given as the following n!=n*(n-1)*(n-2)..(n-n). Which I implemented in C as the following:
unsigned int factorial(int n) {
unsigned int product = 1;
for(int i = n; i > 0; i--) {
product *= i;
}
return product;
}
Then the permutation is given as the following:
Which I implemented as the following in C:
// Given the example of p(5, 3) => 5!/3! which is the same as 5*4, that is how I implemented this below
unsigned int permutation(int N, int n) {
int temp = N-n;
unsigned int product = 1;
for(int i = N; i > temp; i--) { // only compute values from N * (N-1)... (N-n) based on the previous math above
product *= i;
}
return product;
}
The Combination is given as the following, where k is refers to r in above notation:
Which I implemented as the following in C:
int combination(int N, int n) {
unsigned int temp = permutation(N, n);
unsigned int product = factorial(n);
return temp / product;
}
The problem I'm having is I'm trying to take the Combination of nCr(31, 24) = 2629575, which in my C code is the same as permuation(31, 24) / factorial(24) which should equal 1.6315x10^30 / 6.2044x10^23 = 2629575. But I think I'm getting an integer overload because my permutation(31, 24) returns 1279262720, and my factorial(24) returns 3519021056. How do I compute the combination and permutation's of large numbers? Is there a way to store a massive amount of memory aside in C for the factorial and permutations? Lastly, my little TI-30XS calculator can handle these calculations fine, storing numbers up to 1e99, how does it store it?