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

Function bell_numbers

maths/special_numbers/bell_numbers.py:11–45  ·  view source on GitHub ↗

Calculate Bell numbers for the sets of lengths from 0 to max_set_length. In other words, calculate first (max_set_length + 1) Bell numbers. Args: max_set_length (int): The maximum length of the sets for which Bell numbers are calculated. Returns: list: A li

(max_set_length: int)

Source from the content-addressed store, hash-verified

9
10
11def bell_numbers(max_set_length: int) -> list[int]:
12 """
13 Calculate Bell numbers for the sets of lengths from 0 to max_set_length.
14 In other words, calculate first (max_set_length + 1) Bell numbers.
15
16 Args:
17 max_set_length (int): The maximum length of the sets for which
18 Bell numbers are calculated.
19
20 Returns:
21 list: A list of Bell numbers for sets of lengths from 0 to max_set_length.
22
23 Examples:
24 >>> bell_numbers(-2)
25 Traceback (most recent call last):
26 ...
27 ValueError: max_set_length must be non-negative
28 >>> bell_numbers(0)
29 [1]
30 >>> bell_numbers(1)
31 [1, 1]
32 >>> bell_numbers(5)
33 [1, 1, 2, 5, 15, 52]
34 """
35 if max_set_length < 0:
36 raise ValueError("max_set_length must be non-negative")
37
38 bell = [0] * (max_set_length + 1)
39 bell[0] = 1
40
41 for i in range(1, max_set_length + 1):
42 for j in range(i):
43 bell[i] += _binomial_coefficient(i - 1, j) * bell[j]
44
45 return bell
46
47
48def _binomial_coefficient(total_elements: int, elements_to_choose: int) -> int:

Callers

nothing calls this directly

Calls 1

_binomial_coefficientFunction · 0.85

Tested by

no test coverage detected