Skip to content

Instantly share code, notes, and snippets.

@theterminalguy
Created October 21, 2017 19:07
Show Gist options
  • Save theterminalguy/6409a03989b7ee6adb22e3ae9b767486 to your computer and use it in GitHub Desktop.
Save theterminalguy/6409a03989b7ee6adb22e3ae9b767486 to your computer and use it in GitHub Desktop.
Fibonacci sequence in ruby
def fib(n = 5)
first_two = [0, 1]
result = first_two + []
n.times do |i|
ans = first_two.inject(:+)
result.push(ans)
first_two = [first_two.last, ans]
end
result.slice(0, n)
end
fibonacci = Hash.new{ |h,k| h[k] = k < 2 ? k : h[k-1] + h[k-2] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment