Update problem 8 algorithm (faster by 2 orders of magnitude)