Solves #49 in scala