Brute force solution to problem 23
Optimized solution to problem 23