A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Fibonacci in Swift page! Here, you'll find the source code for this program as well as a description of how the program works.
import Foundation
func fibonacciProgram(_ n: Int) {
var f1=0, f2=1, fib=1
for i in 0..<n {
print("\(i+1): \(fib)")
fib = f1 + f2
f1 = f2
f2 = fib
}
}
func fibonacciRecursive(_ n: Int) -> Int {
if (n == 0) {
return 0
} else if (n == 1) {
return 1
}
return fibonacciRecursive(n-1) + fibonacciRecursive(n-2)
}
/*
Check if there is exactly one argument and if it can be parsed as an integer
*/
guard CommandLine.argc == 2, let number = Int(CommandLine.arguments[1]) else {
print("Usage: please input the count of fibonacci numbers to output")
exit(0)
}
fibonacciProgram(number)
Fibonacci in Swift was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.