_max = 10 _end = 1000000 _last = 3628800 locale = _end - 1 num = [1, 1] count = [1,1,1,1,1,1,1,1,1,1] ch = '0123456789' out = '' i = 2 while len(num) < 10: num.append(num[-1] * i) i += 1 for i in xrange(_max - 1, 0, -1): count[i] = locale / num[i] locale %= num[i] out += ch[count[i]] ch = ch[:count[i]] + ch[count[i] + 1:] print out + ch