solve problem 31 with brute force