gcong 2010-03-15
代码如下:
var fibonacci = function (n) { return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2); };
代码如下:
var memoizer = function (memo, fundamental) { var shell = function (n) { var result = memo[n]; if (typeof result !== 'number') { result = fundamental(shell, n); memo[n] = result; } return result; }; return shell; };
代码如下:
var fibonacci = memoizer([0, 1], function (shell, n) { return shell(n - 1) + shell(n - 2); });
代码如下:
var factorial = memoizer([1, 1], function (shell, n) { return n * shell(n - 1); });