Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My initial problem was solved here. It is about enumerating all n-tuples of a permutation in a specific order.

The solution algorithm is very simple and I'm sure has been used before. However, I did not find any scientific references. Do you know it, or do you know how to search for it better?

The code itself:

def seq(n, k):
    if (k == 0):
        yield ()

    for i in range(n+1):
        for subseq in seq(i, k-1):
            for j in range(k):
                yield subseq[:j] + (i,) + subseq[j:]
                if j != k-1 and subseq[j] == i:

Output for n=4, k=2:

10 01 11
20 02 21 12 22
30 03 31 13 32 23 33
40 04 41 14 42 24 43 34 44
share|cite|improve this question
I'm not sure if this is on topic here, if not, please redirect me :) – mafu Sep 12 '12 at 12:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.