Problem 3 solution through trial division