# 6 examples of 'python n choose k' in Python

Every line of 'python n choose k' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure. ## All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
``23def rchoose(n, k):24    if 0 <= n < MAX and 0 <= k < MAX:25        return C[n][k]26    else:27        return choose(n, k)``
``120def choose(n, k):121    if 0 <= k <= n:122        ntok = 1123        ktok = 1124        for t in range(1, min(k, n - k) + 1):125            ntok *= n126            ktok *= t127            n -= 1128        return ntok / ktok129    else:130        return 0``
``1def choose(n, k):2    """3    A fast way to calculate binomial coefficients by Andrew Dalke (contrib).4    """5    if 0 <= k <= n:6        ntok = 17        ktok = 18        for t in xrange(1, min(k, n - k) + 1):9            ntok *= n10            ktok *= t11            n -= 112        return ntok // ktok13    else:14        return 0``
``58def a_choose_b(n,k):59    return reduce(lambda a,b: a*(n-b)/(b+1),xrange(k),1)``
``8def log_choose(n, k):9    r = 0.010    # swap for efficiency if k is more than half of n11    if k * 2 > n:12        k = n - k1314    for  d in range(1,k+1):15        r += numpy.log10(n)16        r -= numpy.log10(d)17        n -= 11819    return r``
``24def __init__(self, n, k):25    """26    TESTS::27    28        sage: from sage.combinat.choose_nk import ChooseNK        29        sage: c52 = ChooseNK(5,2)30        sage: c52 == loads(dumps(c52))31        True32    """33    self._n = n34    self._k = k``