Surprisingly slow Python solution to problem 5.