Solved #3 in Erlang