All the permutations formed by digits 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 and 9 (without repetition) are arranged in increasing order. Which permutation will appear on millionth place?
WARNING: Don't forget about permutations starting with 0 .
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Equivalent code(as the problem is taken from Project Euler) is
1 2 3 4 5 6 7 8 9 10 11 |
|
(2, 7, 8, 3, 9, 1, 5, 4, 6, 0)
Log in to reply
So you solved this question on your own or copied the answer ?
Log in to reply
How could have I copied the answer? I had not answered on Project Euler. So, I have to do it practically on notebook, then on Python.
I think there is a typo. It should be
9 ! ∗ 2 + 8 ! ∗ 6 + 7 ! ∗ 6 + 6 ! ∗ 2 + 5 ! ∗ 5 + 4 ! ∗ 1 + 3 ! ∗ 2 + 2 ! ∗ 2 = 1 0 0 0 0 0 0
Notice the difference in the 2 ! term and there is no 1 ! term.
Can you please make the method easier to understand? Thanks
My understanding of "all" permutations is that you will have permutations of 1 digit, 2 digits, 3 digits and so...
A programmatic solution using Python:
def genPermutation(A):
''' generate all permutations of a given array'''
n = len(A)
if n == 1:
return [A]
result = []
for i in range(n):
a = genPermutation(delete(A, i))
for j in a:
result += [[A[i]] + j]
return result
def delete(A, n):
'''delete value at index i of array A'''
B = A.copy()
L = len(B)
assert(n < L)
for i in range(n, L - 1):
B[i] = B[i+1]
return B[:-1]
arr = genPermutation([0,1,2,3,4,5,6,7,8,9])
print(arr[999999])
Begin with smallest number: 123456789, with 9 digits having:9! numbers. Next is 1023456789, with 1 at first having 9! numbers. Next is 2013456789. So the millionth number has 2 is the first digit. From 2013456789 to 2701345689 having 6*8! numbers, next to 2 7 8 0 1 3 4 5 6 9 : 6 × ( 7 ! ) next to 2 7 8 3 0 1 4 5 6 9 : 2 × ( 6 ! ) to 2 7 8 3 9 0 1 4 5 6 : 5 × ( 5 ! ) to 2 7 8 3 9 1 0 4 5 6 : 4 ! to 2 7 8 3 9 1 5 0 4 6 : 2 × ( 3 ! ) to 2783915460 is millionth number. 2 7 8 3 9 1 5 4 6 0
Problem Loading...
Note Loading...
Set Loading...
9 ! ∗ 2 + 8 ! ∗ 6 + 7 ! ∗ 6 + 6 ! ∗ 2 + 5 ! ∗ 5 + 4 ! ∗ 1 + 3 ! ∗ 2 + 2 ! ∗ 1 + 1 ∗ 1 ! = 9 9 9 9 9 9
This can be found easily by repeated division.
Now, make a table like(where right is the serial number)
9 − 1
8 − 2
7 − 3
6 − 4
5 − 5
4 − 6
3 − 7
2 − 8
1 − 9
0 − 1 0
And cut the number(in the serial number) succeeding the one you see in the multiple of the factorial in the above equation starting from left. For example-
As one can see that multiple of 9 ! is 2 , we cut the number with serial number 3 i.e. 7 and so on.
Carefully note that after you cut any number, you have 1 less than the previous total of numbers left, so serial numbers will change as well, like after first cut,
9 − 1
8 − 2
6 − 3
5 − 4
4 − 5
3 − 6
2 − 7
1 − 8
0 − 9
and so on.
The manner you cut the numbers will be something like - 2 7 8 3 9 1 5 4 6 0 and that's the desired number.