Problem 16 done in Ruby