Solves #48 in scala