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