All problems
Binomial Coefficient
Problem
Compute the binomial coefficient n choose k, the number of ways to choose k items from n distinct items, returning 0 when k < 0 or k > n.
Use the multiplicative formula result = result * (n - k + i) // i for i from 1 to k after replacing k with min(k, n - k). Each step divides exactly, so the result stays an integer.
Hint
Reduce k to min(k, n - k) first so the loop and the numbers stay small.
Test cases
case 1
in: [5,2]
expected: 10
case 2
in: [0,0]
expected: 1
case 3
in: [10,3]
expected: 120
case 4
in: [5,6]
expected: 0
case 5
in: [52,5]
expected: 2598960
Starter codePython
def binomial(n, k):
# Your code here
pass5 test cases · run your code against them in the editor