'Lexicographic permutation unranking.
Examples
>>> from sympy.combinatorics.permutations import Permutation
>>> Permutation.print_cyclic = False
>>> a = Permutation.unrank_lex(5, 10)
>>> a.rank()
10
>>> a
Permutation([0, 2, 4, 1, 3])
See Also
rank, next_lex'

@classmethod
def unrank_lex(cls, size, rank):

    perm_array = ([0] * size)
    psize = 1
    for i in range(size):
        new_psize = (psize * (i + 1))
        d = ((rank % new_psize) // psize)
        rank -= (d * psize)
        perm_array[((size - i) - 1)] = d
        for j in range((size - i), size):
            if (perm_array[j] > (d - 1)):
                perm_array[j] += 1
        psize = new_psize
    return cls._af_new(perm_array)
