Solves #56 in scala