es-next-tools
Version:
A comprehensive utility library for JavaScript and TypeScript that provides a wide range of functions for common programming tasks, including mathematical operations, date manipulations, array and object handling, string utilities, and more.
17 lines (16 loc) • 431 B
JavaScript
import { factorial } from "./factorial";
/**
* Calculates the number of combinations (n choose k).
* @param {number} n - The total number of items.
* @param {number} k - The number of items to choose.
* @returns The number of possible combinations.
* @example
* ```js
* comb(5, 3); // 10
* ```
*/
export function comb(n, k) {
if (k > n)
return 0;
return factorial(n) / (factorial(k) * factorial(n - k));
}