0

Task: Write and test a C++ function that computes binomial coefficients. Three Definitions of the Binomial Coefficients are given (see picture below). I personally used the middle one definition.

My problem: But the Problem is, that I'm getting wrong results. 5 over 2 must be 10 I get 26 instead.

Why not other Definitions of Binomial Coefficient? Because in the other two there is in each definition a fraction and I'm afraid of getting rounding errors by doing divisoin in C++. That's why I chose the Definition in the middle.

My Code:

    #include <iostream>


int binomial(int a,int b){
  if(a < b) return 0;
  else if(a == b || a == 0 ) return 1;

  return binomial(a-1,b) + binomial(a-1,b-1);
}




int main(){

int n; 
int k;
std::cin >> n;
std::cin >> k;

std::cout << "Binomial of " << n << " and " << k << " equals = " << binomial(n,k) << std::endl;

  return 0;
}

Three Definitions of Binomial Coefficient given: Three Definitions of Binomial Coefficient

limonade
  • 45
  • 6

1 Answers1

2

You have the second case wrong

int binomial(int a,int b){
  if(a < b) return 0;
  else if(a == b || a == 0 ) return 1;   //  <------------

  return binomial(a-1,b) + binomial(a-1,b-1);
}

The formula says "if n=k or k=0" and in your code a == n and b == k, so that second line should be

else if(a == b || b == 0 ) return 1;

Note that better naming could have prevented that mistake.

PS

Why not other Definitions of Binomial Coefficient?

Your reasoning is correct for the last definition, but not for the first. The result is always an integer, hence the nominator and denominator are always such that you can carry out the division using integer arithmetics. You will rather run into a problem with getting unnecessarily huge terms for the factorial.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Omg thank you so much ! Do you reccomend that I keep the **n** and the **k** in this particular program even in the function ? – limonade Jan 15 '20 at 19:37
  • @limonade yes definitely. Many if not most mistakes can be prevented by naming things properly. – 463035818_is_not_an_ai Jan 15 '20 at 19:38
  • Okay, I didn't see that the first definition of the Binomial Coefficient always delivers a proper Integer. How can I see such things in future ? And can I keep when using the first definitions the base cases I have in the code above ? – limonade Jan 15 '20 at 19:40
  • @limonade this is now more maths not code. Anyhow... the forumlas are equivalent, if you always get an integer from one you also get an integer from the others ;) – 463035818_is_not_an_ai Jan 15 '20 at 19:41
  • @limonade the first definition is not recursive, you wont need the base case there – 463035818_is_not_an_ai Jan 15 '20 at 19:42
  • @limonade, the key thing to note is that `n!` divides **any** sequence of `n` consecutive integers, i.e. `(k+1)(k+2)...(k+n)` for any `k`. See [this question](https://math.stackexchange.com/questions/11601/proof-that-a-combination-is-an-integer). – Evg Jan 15 '20 at 19:46
  • Oh okay, I tried it now but could not implement as a recursion. :-( – limonade Jan 15 '20 at 19:47
  • If its not recursive, is it then by iteration ? – limonade Jan 15 '20 at 19:47
  • @limonade again: there is no recursion in the first option. If you transform the formula to make it recursive you will end up either with the second or the third option (or variations thereof) – 463035818_is_not_an_ai Jan 15 '20 at 19:48
  • @limonade the factorial can be caluclated in a recursive way, but as I wrote, factorials grow extremely fast and taking the formula (first) literally will not be the best. Thats probably a different question. Consider that for example in 4! / 2! most terms cancel out and the only thing you have to compute is `4*3` – 463035818_is_not_an_ai Jan 15 '20 at 19:50
  • I see, it cancels out. That means when looking at the formula in the first definition, the definition can be rewritten into a different one which is equivalent. – limonade Jan 15 '20 at 19:57
  • @limonade not sure if you understand me. You do have 3 equivalent definitions that can be transformed into each other. Note that maths uses notation that is based on convention. It is just by coincidence that this notation allows to write the definition of the binomials in a convenient way in the three variations you posted. C++ syntax is based on different conventions. In maths writing `102! / 100!` makes complete sense, but when writing code for it any thing but `2` would be non-sense.... hope that makes sense ;) – 463035818_is_not_an_ai Jan 15 '20 at 20:04
  • Okay, thank you so much for your help ! Now I see. :-) – limonade Jan 15 '20 at 20:19