-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmemoizer.js
More file actions
26 lines (21 loc) · 795 Bytes
/
Copy pathmemoizer.js
File metadata and controls
26 lines (21 loc) · 795 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function memoizer(memo, formula) {
var recur = function (n) {
var result = memo[n];
if (typeof result !== 'number') {
result = formula(recur, n);
memo[n] = result;
}
return result;
};
return recur;
};
// The memo variable we pass is an array whose indexes are the inputs to the recursive function
// and whose values are the outputs from the recursive function.
// e.g. the factorial of 0 is 1, and the factorial of 1 is 1, hence the memo array: [1, 1]
var factorial = memoizer([1, 1], function (recur, n) {
return n * recur(n - 1);
});
// The fibonacci of 0 is 0, and fibonacci of 1 is 1, hence the memo array: [0, 1]
var fibonacci = memoizer([0, 1], function (recur, n) {
return recur(n - 1) + recur(n - 2);
});