Created
February 9, 2018 15:55
-
-
Save Nirma/ad197f30ad6f2303e2b5ec54619b6224 to your computer and use it in GitHub Desktop.
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
func recursiveFib(position: Int) -> Int { | |
if position == 0 || position == 1 { | |
return position | |
} | |
return recursiveFib(position: position - 1) + recursiveFib(position: position - 2) | |
} | |
func dynamicFib(position: Int) -> Int { | |
switch position { | |
case 0: | |
return 1 | |
case 1: | |
return 1 | |
default: | |
var cache: [Int] = [1,1] | |
for cursor in 2...position { | |
let next = cache[cursor - 1] + cache[cursor - 2] | |
cache.append(next) | |
} | |
return cache[position] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment