Last active
August 11, 2016 16:19
-
-
Save dbernheisel/c98c3cb30002b10ea336 to your computer and use it in GitHub Desktop.
Return the n numbers in the Fibonacci sequence
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
| def fib(n, sequence = [1, 1]) | |
| return [n] if n == 0 | |
| return [n] if n == 1 | |
| sequence << sequence[-1] + sequence[-2] while sequence.length < n | |
| sequence | |
| end | |
| # fib(0) => [0] | |
| # fib(1) => [1] | |
| # fib(10) => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment