MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / _binomial_coefficient

Function _binomial_coefficient

maths/special_numbers/bell_numbers.py:48–75  ·  view source on GitHub ↗

Calculate the binomial coefficient C(total_elements, elements_to_choose) Args: total_elements (int): The total number of elements. elements_to_choose (int): The number of elements to choose. Returns: int: The binomial coefficient C(total_elements, elements_to_c

(total_elements: int, elements_to_choose: int)

Source from the content-addressed store, hash-verified

46
47
48def _binomial_coefficient(total_elements: int, elements_to_choose: int) -> int:
49 """
50 Calculate the binomial coefficient C(total_elements, elements_to_choose)
51
52 Args:
53 total_elements (int): The total number of elements.
54 elements_to_choose (int): The number of elements to choose.
55
56 Returns:
57 int: The binomial coefficient C(total_elements, elements_to_choose).
58
59 Examples:
60 >>> _binomial_coefficient(5, 2)
61 10
62 >>> _binomial_coefficient(6, 3)
63 20
64 """
65 if elements_to_choose in {0, total_elements}:
66 return 1
67
68 elements_to_choose = min(elements_to_choose, total_elements - elements_to_choose)
69
70 coefficient = 1
71 for i in range(elements_to_choose):
72 coefficient *= total_elements - i
73 coefficient //= i + 1
74
75 return coefficient
76
77
78if __name__ == "__main__":

Callers 1

bell_numbersFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected