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)
| 46 | |
| 47 | |
| 48 | def _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 | |
| 78 | if __name__ == "__main__": |