Solution to problem 24.  Brute force solution.