Problem 18

Does not use brute force