maths.binomial_coefficient

Functions

binomial_coefficient(→ int)

Find binomial coefficient using Pascal's triangle.

Module Contents

maths.binomial_coefficient.binomial_coefficient(n: int, r: int) int

Find binomial coefficient using Pascal’s triangle.

Calculate C(n, r) using Pascal’s triangle.

Parameters:
  • n – The total number of items.

  • r – The number of items to choose.

Returns:

The binomial coefficient C(n, r).

>>> binomial_coefficient(10, 5)
252
>>> binomial_coefficient(10, 0)
1
>>> binomial_coefficient(0, 10)
1
>>> binomial_coefficient(10, 10)
1
>>> binomial_coefficient(5, 2)
10
>>> binomial_coefficient(5, 6)
0
>>> binomial_coefficient(3, 5)
0
>>> binomial_coefficient(-2, 3)
Traceback (most recent call last):
    ...
ValueError: n and r must be non-negative integers
>>> binomial_coefficient(5, -1)
Traceback (most recent call last):
    ...
ValueError: n and r must be non-negative integers
>>> binomial_coefficient(10.1, 5)
Traceback (most recent call last):
    ...
TypeError: 'float' object cannot be interpreted as an integer
>>> binomial_coefficient(10, 5.1)
Traceback (most recent call last):
    ...
TypeError: 'float' object cannot be interpreted as an integer