Solution to prob 18 with brute force method, to be refined