Math.comb libraray

every time I changed value in this library such as (2,3) answer would be 0 please explain this library and its purpose…

I had never used this method before. So taking a look at the the documentation, I found the following:

Return the number of ways to choose k items from n items without repetition and without order.

Evaluates to n! / (k! * (n - k)!) when k <= n and evaluates to zero when k > n .

math. comb ( n , k )
Observe that you have n and k. For this to work n >= k and evaluates to zero when k > n .

So, if you do math.comb(5, 2) you will get a result… but math.comb(2, 5) will give you a zero.

My brain is still trying to understand what to do with this method :roll_eyes:, so as soon as I find a way to understand it (and explain it) I’ll post again. Meanwhile I could explain why you get a zero :grinning:.

A link to the docs:

math.comb(5, 2) will return 20

I managed to understand this concept… and the best way I found to understand it and explain it is thru the following drawing. I’m not 100% this is the correct way to understand the concept, but based on the documentation and a couple of tests, it seems to work. If anybody can confirm or correct me, I’d be happy to accept any corrections. Here is my understanding of the math.com(5,2) code. I hope it makes sense.

Anyway, I still can’t find an application in the real world. I’m sure there is one, but my brain can’t think of one right now. If anybody knows of a real world application for this concept, please share your knowledge.

E.g. to compute the probability of a jackpot in a k-from-n Lotto lottery (1 / math.comb(n, k)). Or to compute how many unique k-person teams you can build with n athletes,…

1 Like

It makes sense. Thanks, @SAM !

Thanks for taking Ur precious time for this problem I understand from the start but in drawing how U cut out 21 31 41 51 32 43 52 43 53 54
could U please clear me this…

@NicolasATC colored the drawing so nicely and even added explanation. 2-1 is discarded because the 2 and the 1 where already used in the 1-2 pair. Same for all other numbers that are striked out in black.