src/coefficients.rs

Sat, 22 Oct 2022 18:12:49 +0300

author
Tuomo Valkonen <tuomov@iki.fi>
date
Sat, 22 Oct 2022 18:12:49 +0300
changeset 1
df3901ec2f5d
parent 0
9f27689eb130
child 5
59dc4c5883f4
permissions
-rw-r--r--

Fix some unit tests after fundamental changes that made them invalid

///! This module implements some arithmetic functions that are valid in a ‘const context”, i.e.,
///! can be computed at compile-time.

/// Calculate $n!$.
/// (The version in the crate `num_integer` deesn't currently work in a `const`  context.
pub const fn factorial(n : usize) -> usize {
    const fn f(i : usize, a : usize) -> usize {
        if i==0 { a } else { f(i-1, i*a) }
    }
    f(n, 1)
}

/// Calculate $\nchoosek{n}{k}$.
/// (The version in the crate `num_integer` deesn't currently work in a `const` context.
pub const fn binomial(n : usize, k : usize) -> usize {
    factorial(n)/(factorial(n-k)*factorial(k))
}

/// Calculate $n^k$.
/// (The version in the crate `num_integer` deesn't currently work in a `const` context.
pub const fn pow(n : usize, k : usize) -> usize {
    const fn pow_accum(n : usize, k : usize, accum : usize) -> usize {
        if k==0 { accum } else { pow_accum(n, k-1, accum*n) }
    }
    pow_accum(n, k, 1)
}

mercurial