Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Commit

Permalink
fixup: Swift example takes arguments from CLI
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Zak <[email protected]>
  • Loading branch information
rjzak committed Jul 28, 2022
1 parent b2d32a7 commit 1483789
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions Swift/fibonacci/fibonacci.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
func fibonacci(n: Int) -> Int {
var a = 0
var b = 1
for _ in 0..<n {
let temp = a
a = b
b = temp + b
func FibonacciSequence(n: Int) -> 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))")

0 comments on commit 1483789

Please sign in to comment.