Changed program to use dict as cache not huge array

Switch from array to dict resulted in huge runtime slowdown, but large
increase in readability.
ToDo: Use tail-recursion to reduce memory footprint