Problem 3, written in Erlang