Same optimization to non-generators version of problem 24