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.
15 lines (14 loc) • 411 B
JavaScript
import { factorial } from "./factorial";
/**
* Calculates the number of permutations (nPk).
* @param {number} n - The total number of items.
* @param {number} [k] - The number of items to arrange.
* @returns The number of possible permutations.
* @example
* perm(3, 2); // 6
*/
export function perm(n, k) {
if (k === undefined || k > n)
return 0;
return factorial(n) / factorial(n - k);
}