Skip to content

Instantly share code, notes, and snippets.

@stevencurtis
Created February 5, 2019 07:59
Show Gist options
  • Save stevencurtis/705b582ddb9793113862d359c91d7cef to your computer and use it in GitHub Desktop.
Save stevencurtis/705b582ddb9793113862d359c91d7cef to your computer and use it in GitHub Desktop.
Fibonacci bottom-up optimised
func fib (_ num: Int) -> Int {
guard num > 1 else { return num }
var a = 0
var b = 1
var c = 0
for _ in 2..<num {
c = a + b
a = b
b = c
}
return a + b
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment