last_max = 0
while i >= max:
    last_max = max
max = n * (max + 1)
digits += 1
result = ''
i -= last_max
while digits:
    digits -= 1
result = seq[i % n] + result
i //= n
return result
How to produce the i-th combination/permutation without iterating