Added solution to problem D in Scala