Created
July 17, 2018 14:48
-
-
Save HARlBO/3f3e48052f201eb41251c551d2aaed58 to your computer and use it in GitHub Desktop.
for문, 재귀를 이용한 피보나치 수 구하기
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 fibonacci(_ n: Int) -> Int { | |
var preValue = 0 | |
var nextValue = 1 | |
for _ in 0 ..< n { | |
let resultValue = preValue + nextValue | |
preValue = nextValue | |
nextValue = resultValue | |
} | |
return nextValue | |
} | |
func fiboRecursive(_ n: Int) -> Int { | |
if n < 2 { return n } | |
return fiboRecursive(n - 1) + fiboRecursive(n - 2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment