diff --git a/Swift/fibonacci/fibonacci.swift b/Swift/fibonacci/fibonacci.swift index 7e36bd9a..9326ae01 100644 --- a/Swift/fibonacci/fibonacci.swift +++ b/Swift/fibonacci/fibonacci.swift @@ -1,12 +1,29 @@ -func fibonacci(n: Int) -> Int { -var a = 0 -var b = 1 -for _ in 0.. Int { + if n <= 1 { + return n + } + + return FibonacciSequence(n: n-1) + FibonacciSequence(n: n-2) +} + +let default_n = 10 + +var n = default_n + +let arguments = CommandLine.arguments + +if (arguments.count > 1) { + n = Int(arguments[1]) ?? default_n +} else { + print("Which Fibonacci index to find? ") + if let line = readLine() { + n = Int(line) ?? default_n + } } -return a + +if n < -1 { + n = default_n } -print(fibonacci(n:7)) + +print("Fibonacci sequence number at index \(n) is \(FibonacciSequence(n: n))")