Lexicographic Permutationsproblem 24
A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations is listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:
012 021 102 120 201 210
What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
Go to the "thread for Problem" in the forum.
Python Code:
a=[0,1,2,3,4,5,6,7,8,9]
def func (x):
I=1
Data=1
While Data<x:
I+=1
Data*=i
Step=int (data/i)
K=1
While x-step*k>0:
K+=1
return [i,k-1,x-step* (k-1)]
Last=[];
Temp= (func (1000000))
While temp[2]>1:
If Temp[0]==len (a):
K=TEMP[1]
Last.append (A[k])
Del A[k]
Else
Last.append (A[0:-temp[0])
Del A[0:-temp[0]]
Last.append (A[temp[1])
Del A[temp[1]]
Temp=func (Temp[2])
If temp[2]==1:
Last.append (A[::-1])
Else
Last.append (a)
Print (last)
Time: <1s
Euler program (python) problem 24