Problem 18 much faster and simpler algorithm with no recursion added