Skip to content

Instantly share code, notes, and snippets.

@AnthonyBY
Last active December 29, 2022 11:44
Show Gist options
  • Save AnthonyBY/6aeffb74b95c61593f10430ed8511689 to your computer and use it in GitHub Desktop.
Save AnthonyBY/6aeffb74b95c61593f10430ed8511689 to your computer and use it in GitHub Desktop.
Recursion: Fibonacci Numbers (Swift 3)
import Foundation
func fibonacci (n: Int) -> Int {
// Write your code here.
if (n == 0 || n == 1) {
return 1;
}
return fibonacci(n: n-1) + fibonacci(n: n-2)
}
// read the integer n
let n = Int(readLine()!)!
// print the nth fibonacci number
print(fibonacci(n: n-1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment