Skip to content

Instantly share code, notes, and snippets.

@odesskij
Created July 26, 2015 18:36
Show Gist options
  • Save odesskij/c2b124fc4a19a41cb377 to your computer and use it in GitHub Desktop.
Save odesskij/c2b124fc4a19a41cb377 to your computer and use it in GitHub Desktop.
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
fibonacci = memoize (n) ->
if n < 2 then n else fibonacci(n - 1) + fibonacci(n - 2)
n = 2
[..., last] = while Math.abs(fibonacci(n) / fibonacci(n - 1) - fibonacci(n - 1) / fibonacci(n - 2)) > 10**-10
n++
fibonacci(n) / fibonacci(n - 1)
console.log n, last
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment