I have function to calculate binomial coefficient with this formula p(n, x) = n!/(n-x)!.x!
As implementation is recursive, I want to stop the further function execution once resultant coefficient exceeds INT_MAX,and it should return -1.
But instead of returning from that case, recursive function considering this return value as computation value return by internal recursive function.
which giving a wrong result.
How can be this handled?
#include <iostream>
#include <cstdlib>
int INT_MAX = 100;
using namespace std;
int binomial_function(int n, int k){
int res = -1;
if (n<k || k<0 ){
return -1;
}
if(k == n || k == 0){
return 1;
}
res = binomial_function(n-1,k-1) + binomial_function(n-1,k);
if (res >= INT_MAX){ // checking overflow of res
return -1;
}
return res;
}
int main(int argc, char *argv[]){
int coefficient = 0;
coefficient = binomial_function(10,5); // actual binomial coeficient is 252 which overflows to INT_MAX
cout<<coefficient<<endl;
coefficient = binomial_function(10,6); // actual binomial coeficient is 210 which overflows to INT_MAX
cout<<coefficient<<endl;
coefficient = binomial_function(10,8); // actual binomial coeficient is 1 which does overflows to INT_MAX
cout<<coefficient<<endl;
return 0;
}
Actual output is:
-2
83
45
Expected output:
-1 >> this should be result because coefficient exceeds INT_MAX
-1 >> this should be result because coefficient exceeds INT_MAX
45 >> this should be result because coefficient does not exceeds INT_MAX