Skip to content

Instantly share code, notes, and snippets.

@balitax
Created October 18, 2019 14:37
Show Gist options
  • Save balitax/d2fff91b12ed011db97816f36810c92b to your computer and use it in GitHub Desktop.
Save balitax/d2fff91b12ed011db97816f36810c92b to your computer and use it in GitHub Desktop.
Fibonacci Swift
import Foundation
func fibonacci(_ number: Int) -> [Int] {
var fibonacci = [Int]()
for n in 0...number {
if n == 0 {
fibonacci.append(0)
} else if n == 1 {
fibonacci.append(1)
} else {
fibonacci.append(fibonacci[n - 1] + fibonacci[n - 2])
}
print("INDEX => ", n, " | FIBBONACI => ", fibonacci[n])
}
return fibonacci
}
print("\n\nFIBO ", fibonacci(15))
INDEX => 0 | FIBBONACI => 0
INDEX => 1 | FIBBONACI => 1
INDEX => 2 | FIBBONACI => 1
INDEX => 3 | FIBBONACI => 2
INDEX => 4 | FIBBONACI => 3
INDEX => 5 | FIBBONACI => 5
INDEX => 6 | FIBBONACI => 8
INDEX => 7 | FIBBONACI => 13
INDEX => 8 | FIBBONACI => 21
INDEX => 9 | FIBBONACI => 34
INDEX => 10 | FIBBONACI => 55
INDEX => 11 | FIBBONACI => 89
INDEX => 12 | FIBBONACI => 144
INDEX => 13 | FIBBONACI => 233
INDEX => 14 | FIBBONACI => 377
INDEX => 15 | FIBBONACI => 610
FIBO [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment