Created
April 8, 2020 15:43
-
-
Save theevo/eee7ef5defb41753b5e155b28265614b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*: | |
| # 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