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
pub fn com(n: i64, mut r: i64) -> i64 {
    if r > n {
        return 0;
    }

    if r * 2 > n {
        r = n - r;
    }

    if r == 0 {
        return 1;
    }

    let mut res = 1;
    for i in 1..(r + 1) {
        res *= n - i + 1;
        res /= i;
    }

    res
}

#[cfg(test)]
mod test_combination {
    use crate::math::com::com;

    #[test]
    fn it_works() {
        assert_eq!(com(6, 0), 1);
        assert_eq!(com(6, 1), 6);
        assert_eq!(com(6, 2), 15);
        assert_eq!(com(6, 3), 20);
        assert_eq!(com(6, 4), 15);
        assert_eq!(com(6, 5), 6);
        assert_eq!(com(6, 6), 1);
    }
}