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