Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function binomial(n, k, e) {
- /**
- **If e is even calculating for combinations with repetitions - (n+k-1)! / (k! * (n-1)!)
- **Else for combinations without repetitions - n! / (k! * (n-k)!)
- ***/
- var numerator = 1, denominator = 1;
- if (k > n - k && e % 2 === 1) k = n - k; //possible reducing k if looking for combinations without repetitions
- for (var i = 0; i < k; i++) {
- numerator *= n + i * Math.pow(-1, e);
- denominator *= k - i;
- }
- return formatting(numerator / denominator);
- }
- function formatting(string) {
- return string.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
- }
- console.log(binomial(10, 8, 1));
- console.log(binomial(10, 8, 2));
Advertisement
Add Comment
Please, Sign In to add comment