Skip to content

Instantly share code, notes, and snippets.

@Nirma
Created February 9, 2018 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nirma/ad197f30ad6f2303e2b5ec54619b6224 to your computer and use it in GitHub Desktop.
Save Nirma/ad197f30ad6f2303e2b5ec54619b6224 to your computer and use it in GitHub Desktop.
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