I implemented this as a gem which can be found at https://github.com/tilthouse/scf_exercise For much higher performance, there exist native gems, We could also use the 'job_interview' gem: https://github.com/ruby-jokes/job_interview
FWIW, this seemed familiar. Turns out I did this problem from Project Euler in 2012. I did not create a gem for it at that time. My "get the answer quickly" solution from the time is at https://github.com/tilthouse/project_euler/blob/master/problems/002/answer002.rb I did look this up after doing this exercise again. My previous solution did not use caching, for example.