Skip to content

Instantly share code, notes, and snippets.

@toshi0383
Created December 10, 2015 14:25
Show Gist options
  • Select an option

  • Save toshi0383/12c781a5b32173107520 to your computer and use it in GitHub Desktop.

Select an option

Save toshi0383/12c781a5b32173107520 to your computer and use it in GitHub Desktop.
func fib(idx:Int) {
let result = (0...idx).reduce([Int]()) {(var t, e) in
t.append(t.count < 2 ? e : t[t.count-2] + t[t.count-1])
return t
}.last
print(result ?? "no value found")
}
_ = Process.arguments
.map{Int($0)}.flatMap{$0}.filter{$0>=0}.map(fib)
/// usage:
/// $ swift fib.swift 1 2 3 4 5 6 7 8 9 10
/// 1
/// 1
/// 2
/// 3
/// 5
/// 8
/// 13
/// 21
/// 34
/// 55
@toshi0383

Copy link
Copy Markdown
Author

finding fibonacci number by index

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment