Brute force approach to problem three