@onesy/algorithms
Version:
19 lines (18 loc) • 676 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const cache = {};
function factorial(value, options = { cache: true }) {
// Cache
if ((options === null || options === void 0 ? void 0 : options.cache) && cache[value] !== undefined)
return cache[value];
if (value < 3)
return value;
const values = [0, 1, 2];
for (let i = 3; i <= value; i++)
values[i] = i * values[i - 1];
const result = values[values.length - 1];
if ((options === null || options === void 0 ? void 0 : options.cache) && cache[value] === undefined)
cache[value] = result;
return result;
}
exports.default = factorial;