0 votes
1 view
in Python by (9.5k points)

I'm hoping to check whether worked in with the math library in python is the nCr (n Choose r) function

I comprehend that this can be customized yet I felt that I'd verify whether it's as of now built-in before I do.

1 Answer

0 votes
by (19.7k points)

Look at the following code, which calculates nCr in a good manner

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  # or / in Python 2

In the Standard library, You can see binomial  coefficients as math.comb (In python 3.8):

>>> from math import comb

>>> comb(10,3)

120

Join the python training course, if you're much interested to learn python!!

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...