Created
October 13, 2020 05:28
-
-
Save aershov24/8a4c90bb54e79d8c36921d7e76b7d35d to your computer and use it in GitHub Desktop.
Markdium-14 Fibonacci Interview Questions (SOLVED) To Brush Before Coding Interview
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
| // Iterative Solution for Fibonacci Numbers | |
| function *fibonacci(n) { | |
| const infinite = !n && n !== 0; | |
| let current = 0; | |
| let next = 1; | |
| while (infinite || n--) { | |
| yield current; | |
| [current, next] = [next, current + next]; | |
| } | |
| } | |
| // Recursive Solution for Fibonacci Numbers | |
| function *fibonacci(n = null, current = 0, next = 1) { | |
| if (n === 0) { | |
| return current; | |
| } | |
| let m = n !== null ? n - 1 : null; | |
| yield current; | |
| yield *fibonacci(m, next, current + next); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment