Created
March 19, 2017 03:08
-
-
Save camillevilla/9ff460ae094d5a0ff978ad7dc95c73a2 to your computer and use it in GitHub Desktop.
Fibonacci with Ruby's .reduce
This file contains 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
# find the nth value of the Fibonacci sequence | |
def fibonacci(nth) | |
counter = 1 | |
current_arr = [0, 1] | |
until counter == nth - 1 | |
next_val = current_arr.reduce(0) {|sum, number| sum + number } | |
current_arr = [current_arr.last, next_val] | |
counter += 1 | |
end | |
current_arr.last | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment