Skip to content

Instantly share code, notes, and snippets.

@Nirma
Created Feb 9, 2018
Embed
What would you like to do?
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