Solved problem 12 by optimizing num_factors.