Created
January 8, 2018 10:04
-
-
Save Libbum/fbc4196bf48fbb2a9e193034edc1bbfb to your computer and use it in GitHub Desktop.
Binomial Coefficient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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