Solves #25 in scala