Last active
April 8, 2020 16:02
-
-
Save theevo/f3f315b387f15e57d0cd8ca0d14070b3 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(under target: Int) -> Int { | |
| guard target > 0 else { return 0 } | |
| if target == 1 { return 1 } | |
| var a: Int = 0 | |
| var b: Int = 1 | |
| var sum: Int = 1 | |
| while b < target { | |
| print("\(a) \(b)") | |
| a = b | |
| b = sum | |
| sum = a + b | |
| } | |
| return a | |
| } | |
| fibbonacci(under: 0) // 0 | |
| fibbonacci(under: 1) // 1 | |
| fibbonacci(under: 7) // 5 | |
| fibbonacci(under: 2000) // 1597 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment