Skip to content

Instantly share code, notes, and snippets.

@Libbum
Created January 8, 2018 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Libbum/fbc4196bf48fbb2a9e193034edc1bbfb to your computer and use it in GitHub Desktop.
Save Libbum/fbc4196bf48fbb2a9e193034edc1bbfb to your computer and use it in GitHub Desktop.
Binomial Coefficient
/// Calculate the number of combinations when choosing k values from n elements.
fn nchoosek(n: u64, k: u64, scale: f64) -> f64 {
if k > n {
0.0
} else if k == 0 || k == 1 {
1.0
} else {
let mut comb = 1.0;
for j in 0..cmp::min(k, n - k) {
comb /= (j + 1) as f64;
comb *= (n - j) as f64;
}
comb
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment