New version of Python script.

Two versions. One of them includes memoization.