Function malachite_base::vecs::exhaustive::lex_k_compositions

source ·
pub fn lex_k_compositions(n: usize, k: usize) -> LexKCompositions 
Expand description

Generates all $k$-compositions of a number: given $n$ and $k$, generates all length-$k$ Vecs of positive usizes whose sum is $n$.

The Vecs are output in lexicographic order.

If $k = 0$ and $n \neq 0$, or if $n < k$, then the output is empty.

The output length is $$ \binom{n-1}{k-1}. $$

§Examples

use itertools::Itertools;
use malachite_base::vecs::exhaustive::lex_k_compositions;

let xss = lex_k_compositions(5, 3).collect_vec();
assert_eq!(
    xss.iter().map(Vec::as_slice).collect_vec().as_slice(),
    &[&[1, 1, 3], &[1, 2, 2], &[1, 3, 1], &[2, 1, 2], &[2, 2, 1], &[3, 1, 1]]
);