Skip to content

Instantly share code, notes, and snippets.

@chrisross
Last active February 23, 2023 23:25
Show Gist options
  • Save chrisross/daa9acfb1aff9aeaf109ebfd154cd078 to your computer and use it in GitHub Desktop.
Save chrisross/daa9acfb1aff9aeaf109ebfd154cd078 to your computer and use it in GitHub Desktop.
Simple Fibonacci sequence generator using Ruby
def fibonacci(limit = 10)
limit.times.each_with_object([0,1]) do |elem, accum|
accum << accum[-1] + accum[-2]
end
end
# Examples
fibonacci
#=> [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
fibonacci(15)
#=> [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment