How does this return statement with two recursive calls work?

Viewed 58

I'm trying to understand this binomial coefficient recursion code in C.

int binomial(int n, int m){
    if(m == n){
        return 1;
    }
    else if (m == 0){
        return 1;
    }
    else if (m>n){
        return 0;
    }
    else{
        return binomial(n-1,m-1) + binomial(n-1,m);
    }
}

I understand everything besides the final line return binomial(n-1,m-1) + binomial(n-1,m);. If anyone is able to explain what is happening I'd appreciate it. I know that actually understanding what the code is doing helps in improving your coding ability so that's why I wanna know.

1 Answers

Following are common definition of Binomial Coefficients :

  1. A binomial coefficients C(n, k) can be defined as the coefficient of X^k in the expansion of (1 + X)^n.

  2. A binomial coefficients C(n, k) also gives the number of ways, disregarding order, that k objects can be chosen from among n objects; more formally, the number of k-element subsets (or k-combinations) of an n-element set.

Related