Skip to content

Instantly share code, notes, and snippets.

@theevo
Created April 8, 2020 15:43
Show Gist options
  • Select an option

  • Save theevo/eee7ef5defb41753b5e155b28265614b to your computer and use it in GitHub Desktop.

Select an option

Save theevo/eee7ef5defb41753b5e155b28265614b to your computer and use it in GitHub Desktop.
/*:
# Wednesday Stretch Problem 6.3
## Fibbonacci Number
### Instructions:
1. Create a function that finds the closest fibonacci number that is less than or equal to the number that is passed into a function.
2. Don't hesitate to Google what a Fibonacci number is.
3. Test it by passing in the number 2000. We will compare results.
### Black Diamond πŸ’ŽπŸ’ŽπŸ’Ž
Create the above using a single line of code in the function body. (Hint: recursion)
*/
import Foundation
func fibbonacci(_ target: Int) -> Int {
guard target > 0 else { return 0 }
var a: Int = 0
var b: Int = 1
var c: Int = 1
while b < target {
print("\(a) \(b)")
a = b
b = c
c = a + b
}
return a
}
fibbonacci(0)
fibbonacci(1)
fibbonacci(7)
fibbonacci(2000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment