Here's a recursive implementation of finding the nth Fibonacci number in Ruby:
main.rb106 chars7 lines
However, this implementation has an exponential time complexity, which means that it becomes very slow for large values of n.
A more efficient approach is to use memoization to store the results of previous calculations so they can be reused later. Here's an implementation:
main.rb153 chars9 lines
This implementation has a linear time complexity and is much faster for large values of n.
gistlibby LogSnag