-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathbinomial_coefficient.rs
75 lines (65 loc) · 1.66 KB
/
binomial_coefficient.rs
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
extern crate num_bigint;
extern crate num_traits;
use num_bigint::BigInt;
use num_traits::FromPrimitive;
/// Calculate binomial coefficient (n choose k).
///
/// This function computes the binomial coefficient C(n, k) using BigInt
/// for arbitrary precision arithmetic.
///
/// Formula:
/// C(n, k) = n! / (k! * (n - k)!)
///
/// Reference:
/// [Binomial Coefficient - Wikipedia](https://en.wikipedia.org/wiki/Binomial_coefficient)
///
/// # Arguments
///
/// * `n` - The total number of items.
/// * `k` - The number of items to choose from `n`.
///
/// # Returns
///
/// Returns the binomial coefficient C(n, k) as a BigInt.
pub fn binom(n: u64, k: u64) -> BigInt {
let mut res = BigInt::from_u64(1).unwrap();
for i in 0..k {
res = (res * BigInt::from_u64(n - i).unwrap()) / BigInt::from_u64(i + 1).unwrap();
}
res
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_binom_5_2() {
assert_eq!(binom(5, 2), BigInt::from(10));
}
#[test]
fn test_binom_10_5() {
assert_eq!(binom(10, 5), BigInt::from(252));
}
#[test]
fn test_binom_0_0() {
assert_eq!(binom(0, 0), BigInt::from(1));
}
#[test]
fn test_binom_large_n_small_k() {
assert_eq!(binom(1000, 2), BigInt::from(499500));
}
#[test]
fn test_binom_random_1() {
// Random test case 1
assert_eq!(binom(7, 4), BigInt::from(35));
}
#[test]
fn test_binom_random_2() {
// Random test case 2
assert_eq!(binom(12, 3), BigInt::from(220));
}
#[test]
fn test_binom_random_3() {
// Random test case 3
assert_eq!(binom(20, 10), BigInt::from(184_756));
}
}