Given:
- A list of symbols of size M
- The desired size of combination L
- Symbols may occur any number of times in a combination
- All permutations of any combination of the symbols must be taken into the account
Example: for a list of symbols (a, b, c)
, and L=4, all of the combinations (a, a, a, a)
, (a, b, a, c)
, (a, c, b, b)
and so on are valid. For the lack of a better term, I called this "loose combinations".
The particular ordering of the combinations is not important. Being given the combination index N, the algorithm should return a unique combination from the set of possible combinations that satisfy the conditions. My guess is that the most natural order would be if we consider the combinations as numbers of radix M and length L, so that the normal number order would apply, but that is not strictly necessary to follow.
What is the algorithm to find the Nth combination?
I'm not sure how to find the answer myself, and have been searching if there was an answer for this particular set of conditions elsewhere, but did not find it. All the questions that I find are not interested in combinations with repeating elements like (a, a, b, b)
and combinations with rearranged order, like (a, a, b, c)
and (a, b, c, a)
or (a, c, a, b)
are treated as the same combination.
for loop
" - leonardkraemerN < M^L
right? - leonardkraemer