Problem 13 in scala