UNPKG

ts-math

Version:

A collection of math functions and packages written in Typescript

51 lines 1.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.permute = exports.rotate = exports.fact = void 0; /** * The factorial of n. */ function fact(n) { var res = 1; for (let i = 2; i <= n; i++) { res *= i; } return res; } exports.fact = fact; /** * Rotate the elements in array from index n to m. Example [0,1,2,3,4,5], 2,4 => [0,1,4,2,3,5] * @param {Array} arr - The array to rotate * @param {int} n - Start index * @param {int} m - End Index (including) */ function rotate(arr, n, m) { let s = arr[m]; for (let i = m - 1; i >= n; i--) { arr[i + 1] = arr[i]; } arr[n] = s; } exports.rotate = rotate; /** * Permutes an array. * @param {*} arr - The array to permute * @param {*} idx - The permutation index. An index between 0 and fact(arr.length)-1 */ function permute(arr, idx) { var i = 0; var d = fact(arr.length - 1); var r = idx; var n; while (i < arr.length - 1) { n = r; r %= d; n = (n - r) / d; rotate(arr, i, i + n); i++; d /= arr.length - i; } rotate(arr, i, i + r); return arr; } exports.permute = permute; //# sourceMappingURL=permute.js.map