Well, there are a few obvious brute force solutions. The most obvious be run through all 1000000 combinations and find ones containing #s 1-6 w/ no repeats. Of course…not elegant at all, but let’s keep going.
Next solution is iterative. This time we have to represent our permutations as arrays of integers. We’re gonna traverse all possibilities for each index, but any time we have duplicate indices, we skip. So all permutations starting with 1,1 are immediately ignored. Observe:
for(int a=1;a<=6;a++) {
for(int b=1;b<=6;b++) {
if(b==a) { continue; }
for(int c=1;c<=6;c++) {
if(c==a || c==b) { continue; }
for(int d=1;d<=6;d++) {
if(d==a || d==b || d==c) { continue; }
for(int e=1;e<=6;e++) {
if(e==a || e==b || e==c || e==d) { continue; }
int f=(1+2+3+4+5+6) - (a+b+c+d+e);
//add permutation a,b,c,d,e,f to your list
//I'm being ambiguous here because there are many ways to store your list
}
}
}
}
}
You might also notice that we didn’t even have to use loops for the 6th index because we instead just found whichever one hadn’t been used yet. The whole adding and subtracting method was the first thing that came to mind, but there’s probably another better way of doing that. I think this involves 2586 iterations, which is good, but might not be the best.
The other method I came up with, and arguably a more elegant method, involves recursion.
Ultimately, if you want all permutations of numbers 1-6, that’s the same as concatenating the following 6 sets together:
Take all permutations of 1,2,3,4,5. Then put a 6 at the end of each permutation.
Take all permutations of 1,2,3,4,6. The put a 5 at the end of each permutation.
Take all permutations of 1,2,3,5,6. Then put a 4 at the end of each permutation.
…
Take all permutations of 2,3,4,5,6. Then put a 1 at the end of each permutation.
This ultimately involves taking all permutations of 5 different numbers, and repeating that 6 times, just with different sets. Taking all permutations of 5 different numbers can be broken down further, into 5 sets, each containing permutations of 4 numbers with another number slapped on at the end. And so on until you reach the trivial combinations of 2 numbers. This is probably more efficient than the iterative method. Or at least, if not more efficient, it’s at least reusable.
That said, if you really want, I might be able to write code for this recursive method. I started doing it, then realized it involves a bunch of tedious array functions and realized the first thing I gave you should work just fine.
Also, I realized that there’s a possible speedup to the recursive method. It’d probably cut the work load down by a lot. Instead of creating 6 unique sets of permutations of 5 numbers, we could just generate one set, then swap the number at the end with one other number. This could cut the work load down a lot, or could make things slower because of how much sequential searching it involves.
Honestly, if you apply that one speedup, you really eliminate all the trial and error and only ever have to work with combinations guaranteed to work. And, as a bonus, if you decide to slap the number before the permutation instead of after, your permutations come in the exact same order they did in the iterative method. Honestly, the only downside is all the array accesses, which could make or break the overall efficiency.
There are probably many different ways I didn’t even think of to approach this. At heart, this problem feels like a classical conundrum with multiple different solutions, like sorting algorithms. Which is why I’m surprised I never ran into this problem until now.
Anyway, feel free to use the code I supplied. Have a good one!