Solved problems 51, 52 in scala