Created
February 5, 2019 07:40
-
-
Save stevencurtis/827e03cd00fd83ab65aa317c8c0167ab to your computer and use it in GitHub Desktop.
Fibonacci top down
This file contains 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
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