forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursive_binomial_coefficient.py
More file actions
42 lines (32 loc) · 1019 Bytes
/
recursive_binomial_coefficient.py
File metadata and controls
42 lines (32 loc) · 1019 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
Recursive Binomial Coefficient
Calculate the binomial coefficient C(n, k) using a recursive formula with
the identity C(n, k) = (n/k) * C(n-1, k-1).
Reference: https://en.wikipedia.org/wiki/Binomial_coefficient
Complexity:
Time: O(k)
Space: O(k) recursive stack
"""
from __future__ import annotations
def recursive_binomial_coefficient(n: int, k: int) -> int:
"""Calculate C(n, k) recursively, where n >= k.
Args:
n: Total number of items.
k: Number of items to choose.
Returns:
The binomial coefficient C(n, k).
Raises:
ValueError: If k > n.
Examples:
>>> recursive_binomial_coefficient(5, 0)
1
>>> recursive_binomial_coefficient(8, 2)
28
"""
if k > n:
raise ValueError("Invalid Inputs, ensure that n >= k")
if k == 0 or n == k:
return 1
if k > n / 2:
return recursive_binomial_coefficient(n, n - k)
return int((n / k) * recursive_binomial_coefficient(n - 1, k - 1))