A standard permutation/combination problem in some coding tests. You are often required to iterate all of them.

Given N cities, how many permutations of any combinations are there in total.

My iterative sum formula: answer(N)= N_choose_0 + N_choose_1 * 1! + N_choose_2 * 2! + … + N_choose_N * N!

N_choose_0 = 1 !

–algo: use answer(N-1) to answer(N)

–iterative algo:

- Suppose we have an iterative_next_perm(list) function already written.
- suppose we have an iterative_next_combo(N, 3) that generates all combinations of 3 out of N distinct chars.

Solution: call

iterative_next_combo(N,1)

iterative_next_combo(N,2)

iterative_next_combo(N,3)… in a loop (of N iterations).

Suppose a call generated 221 combinations. Run a loop (of 221 iterations) each time pass the combination into iterative_next_perm(combo)

So our main program has only 2 nested loops. Most of the complexities are encapsulated in iterative_next_perm() and iterative_next_combo()