-1

I need help to construct a python program!

Ok so I have to write a code that solves binomial coefficients with the following formula. What it basically says is the same as the sum from j to i, just that it multiplicates. This is a generalization of the binomial theorem.

So do anyone have a good idea of how to write a python code that uses this formula to print out binomial coefficients. The variables are n and i. Formula below:

'equation'

David Buck
  • 3,752
  • 35
  • 31
  • 35
  • 1
    what have you tried so far? – heittpr Sep 20 '20 at 23:08
  • 1
    Please don't make more work for others by vandalizing your posts. By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under a [CC BY-SA license](//creativecommons.org/licenses/by-sa/4.0), for SE to distribute the content (i.e. regardless of your future choices). By SE policy, the non-vandalized version is distributed. Thus, any vandalism will be reverted. Please see: [How does deleting work? …](//meta.stackexchange.com/q/5221). If permitted to delete, there's a "delete" button below the post, on the left, but it's only in browsers, not the mobile app. – Makyen Sep 23 '20 at 19:07

2 Answers2

1

Use a variable to accumulate the answer, and then at each iteration, multiply it by (n - j + 1)/j

def choose(n, i):
    ans = 1

    for j in range(1, i+1):
        ans *= (n - j + 1)/j
    
    return ans

print(choose(7, 5)) # 21

For larger values, this function will return inf, since we're using float rather than int. We can instead compute the numerator and the denominator and then perform integer division with them:

def choose(n, i):
    i = min(i, n-i)
    numer = 1
    denom = 1

    for j in range(1, i+1):
        numer *= (n - j + 1)

    for j in range(1, i+1):
        denom *= j
    
    return numer // denom

print(choose(100000, 99940)) # 1180691979962...

Note that for such large numbers it's better to do i = min(i, n-i) since this can speed up performance considerably and n choose i = n choose (n-i).

heittpr
  • 591
  • 4
  • 9
  • is there an alternative method to use when i>n/2?? – ROLLIN40SCRIPS Sep 21 '20 at 09:57
  • @DavidOmanovic what do you mean? This method works for i larger than n/2 and it's implemented based on the formula you provided. – heittpr Sep 21 '20 at 12:22
  • i get inf as answer when i for instance do n=100000 and i=99940 but the answer is 18069197996257 · 10^218, why so, and how do i fix it? – ROLLIN40SCRIPS Sep 21 '20 at 15:45
  • That doesn't happen because `i > n/2` (there are many small cases where that is true and the function returns the correct value), but rather because we're using float instead of integer division, so for large enough values, the result will be converted to `inf`. – heittpr Sep 21 '20 at 19:00
0

The answer can be a brute force approach as

n = 3
i = 2
ans = 1
for j in range(1, i + 1):
    ans *= (n - j + 1) / j
ans

Or, on simplifying, this is basically, nCr formula you are trying to implement. So,

import operator as op
from functools import reduce

def ncr(n, r):
    r = min(r, n-r)
    numer = reduce(op.mul, range(n, n-r, -1), 1)
    denom = reduce(op.mul, range(1, r+1), 1)
    return numer // denom 

If you are using Python 3.8 or above, the comb module is now included in the math library. Do simply

from math import comb
comb(3, 2)
Swati Srivastava
  • 1,102
  • 1
  • 12
  • 18
  • is there an alternative method to use when i>n/2?? – ROLLIN40SCRIPS Sep 21 '20 at 09:57
  • @DavidOmanovic this method works for all values of i as long as it is less than n. If you want to start from ```i=n/2```, you can initialize ```i = n / 2``` in the first snippet. The other 2 snippets calculate nCr value – Swati Srivastava Sep 21 '20 at 21:40