Skip to content

Instantly share code, notes, and snippets.

@stevencurtis
Created February 5, 2019 07:40
Show Gist options
  • Save stevencurtis/827e03cd00fd83ab65aa317c8c0167ab to your computer and use it in GitHub Desktop.
Save stevencurtis/827e03cd00fd83ab65aa317c8c0167ab to your computer and use it in GitHub Desktop.
Fibonacci top down
var cache : [Int] = [0]
func fib (_ num: Int) -> Int {
guard num > 1 else { return num }
if (cache.count > num) {
return cache[num]
}
let fibNum = fibRec(num - 1) + fibRec(num - 2)
cache.append(fibNum)
return fibNum
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment