intermediate result for problem18