problem039 in scala