solved problem 40 in Erlang