Solves #28 in scala