What is the correct (optimal) way of generating all possible ordered k-tuples of digits from 0 to 9 in which no elements repeat and the first element is nonzero?
My current code
k=3;
Digs = Select[Tuples[Prepend[Table[Range[0, 9], k - 1], Range[1, 9]]],
CountDistinct[#] == k &]
works only for small k
but then runs out of memory because it generates too many unnecessary tuples.
I found a related answer here which provides a uniqueTuples
function but it only works for a pair of lists and not for an arbitrary number of lists. Can that be generalized to an arbitrary number of lists somehow in a way to avoid the Select
applied to a huge wasteful set?
In the special case k=10
it should just return the permutations of Range[0, 9]
excluding those that start with 0, which is still a manageable number.