Improve performance of Python's problem 206.