Created
July 26, 2015 18:09
-
-
Save odesskij/f0b02ee2872598fd00da to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fibMemoize = (n) -> | |
memo = [1, 1] | |
for i in [2...n] | |
memo[i] = memo[i - 1] + memo[i - 2] | |
memo[n - 1] | |
console.log fibMemoize 10 | |
fibReq = (n) -> | |
if n < 2 then n else fibReq(n - 1) + fibReq(n - 2) | |
console.log fibReq 10 | |
memoize = (func, hashFunc) -> | |
memo = {} | |
hashFunc = hashFunc or (n) -> n | |
() -> | |
key = hashFunc.apply @, arguments | |
if memo[key]? then memo[key] else memo[key] = func.apply @, arguments | |
console.log '------------' | |
fibReqMemoize = memoize (n) -> | |
if n < 2 then n else fibReqMemoize(n - 1) + fibReqMemoize(n - 2) | |
console.log fibReqMemoize 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment