Last active
June 8, 2019 20:36
-
-
Save davidinga/65ea5cee181e7d0028d8ae2724f032a5 to your computer and use it in GitHub Desktop.
Returns the nth number in the Fibonacci Sequence.
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 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