fixed recursion counter in iterative algorithm, reduced cache use in recursive algorithm