Skip to content

Instantly share code, notes, and snippets.

@HARlBO
Created July 17, 2018 14:48
Show Gist options
  • Save HARlBO/3f3e48052f201eb41251c551d2aaed58 to your computer and use it in GitHub Desktop.
Save HARlBO/3f3e48052f201eb41251c551d2aaed58 to your computer and use it in GitHub Desktop.
for문, 재귀를 이용한 피보나치 수 구하기
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