Skip to content

Instantly share code, notes, and snippets.

@davidinga
Last active June 8, 2019 20:36
Show Gist options
  • Save davidinga/65ea5cee181e7d0028d8ae2724f032a5 to your computer and use it in GitHub Desktop.
Save davidinga/65ea5cee181e7d0028d8ae2724f032a5 to your computer and use it in GitHub Desktop.
Returns the nth number in the Fibonacci Sequence.
func iterativeFib(_ n: Int) -> Int {
var n1 = 1, n2 = 1, answer = 0, temp = 0
for _ in 2..<n {
answer = n1 + n2
temp = n2
n2 = answer
n1 = temp
}
return answer
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment