problem #3 solved using Ruby
problem 4, in Ruby