added a memoized version of generating permuations