Code for algorithms in Ruby