fibonacci数列
有记忆功能的fibonacci数列
var memorizer = function( 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 ;
}
var fibonacci = memorizer( [0,1], function (recur, n) {
return recur( n - 1 ) + recur( n - 2 ) ;
})
var str = fibonacci( 4 ) ;
var recur = function( n ){
var result = memo[ n ] ;
if( typeof result != "number" ){
result = formula( recur, n ) ;
memo[ n ] = result ;
}
return result ;
}
return recur ;
}
var fibonacci = memorizer( [0,1], function (recur, n) {
return recur( n - 1 ) + recur( n - 2 ) ;
})
var str = fibonacci( 4 ) ;