I think it could be improved by not calling the task "permutations". Permutations are rearrangements. It seems you are mostly producing all combinations. I refer you to these: * http://www.cplusplus.com/reference/algorithm/next_permutation/ * http://en.wikipedia.org/wiki/Permutation