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