Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Powerset count #735

Merged
merged 11 commits into from
Aug 26, 2023
8 changes: 7 additions & 1 deletion src/powerset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<I> Iterator for Powerset<I>
fn count(self) -> usize {
let k = self.combs.k();
let (n, combs_count) = self.combs.n_and_count();
combs_count + (k + 1..=n).map(|i| checked_binomial(n, i).unwrap()).sum::<usize>()
remaining_for(combs_count, n, k).unwrap()
}
}

Expand All @@ -70,3 +70,9 @@ impl<I> FusedIterator for Powerset<I>
I: Iterator,
I::Item: Clone,
{}

fn remaining_for(init_count: usize, n: usize, k: usize) -> Option<usize> {
Philippe-Cholet marked this conversation as resolved.
Show resolved Hide resolved
(k + 1..=n).fold(Some(init_count), |sum, i| {
sum.and_then(|s| s.checked_add(checked_binomial(n, i)?))
})
}