Skip to content

Instantly share code, notes, and snippets.

@camillevilla
Created March 19, 2017 03:08
Show Gist options
  • Save camillevilla/9ff460ae094d5a0ff978ad7dc95c73a2 to your computer and use it in GitHub Desktop.
Save camillevilla/9ff460ae094d5a0ff978ad7dc95c73a2 to your computer and use it in GitHub Desktop.
Fibonacci with Ruby's .reduce
# 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