Permutations

All the permutations formed by digits 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 0,1,2,3,4,5,6,7,8 and 9 9 (without repetition) are arranged in increasing order. Which permutation will appear on millionth place?

WARNING: Don't forget about permutations starting with 0 0 .


The answer is 2783915460.

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.

3 solutions

Kartik Sharma
Mar 11, 2015

9 ! 2 + 8 ! 6 + 7 ! 6 + 6 ! 2 + 5 ! 5 + 4 ! 1 + 3 ! 2 + 2 ! 1 + 1 1 ! = 999999 9!*2 + 8!*6 + 7!*6 + 6!*2 + 5!*5 + 4!*1 + 3!*2 + 2!*1 + 1*1! = 999999

This can be found easily by repeated division.

Now, make a table like(where right is the serial number)

9 1 9 - 1

8 2 8 - 2

7 3 7 - 3

6 4 6 - 4

5 5 5 - 5

4 6 4 - 6

3 7 3 - 7

2 8 2 - 8

1 9 1 - 9

0 10 0 - 10

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 ! 9! is 2 2 , we cut the number with serial number 3 3 i.e. 7 7 and so on.

Carefully note that after you cut any number, you have 1 1 less than the previous total of numbers left, so serial numbers will change as well, like after first cut,

9 1 9 - 1

8 2 8 - 2

6 3 6 - 3

5 4 5 - 4

4 5 4 - 5

3 6 3 - 6

2 7 2 - 7

1 8 1 - 8

0 9 0 - 9

and so on.

The manner you cut the numbers will be something like - 2783915460 2783915460 and that's the desired number.

Equivalent code(as the problem is taken from Project Euler) is

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
>>> import itertools 

>>> R = range(0,10)

>>> L = []

>>> for i in itertools.permutations(R):

    L.append(i)

>>> L[999999]

(2, 7, 8, 3, 9, 1, 5, 4, 6, 0)

Kartik Sharma - 6 years, 3 months ago

Log in to reply

So you solved this question on your own or copied the answer ?

Uchiha Robert - 6 years, 3 months ago

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.

Kartik Sharma - 6 years, 3 months ago

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 = 1000000 9!*2+8!*6+7!*6+6!*2+5!*5+4!*1+3!*2+2!*2 = 1000000

Notice the difference in the 2 ! 2! term and there is no 1 ! 1! term.

Pawan Kumar - 6 years, 2 months ago

Can you please make the method easier to understand? Thanks

Ritu Roy - 5 years, 7 months ago

My understanding of "all" permutations is that you will have permutations of 1 digit, 2 digits, 3 digits and so...

Roman Frago - 6 years, 2 months ago
Canwen Jiao
Aug 5, 2018

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])
Aaaaa Bbbbb
Mar 11, 2015

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 2780134569 : 6 × ( 7 ! ) 2780134569: 6 \times (7!) next to 2783014569 : 2 × ( 6 ! ) 2783014569: 2 \times(6!) to 2783901456 : 5 × ( 5 ! ) 2783901456: 5 \times(5!) to 2783910456 : 4 ! 2783910456: 4! to 2783915046 : 2 × ( 3 ! ) 2783915046: 2 \times(3!) to 2783915460 is millionth number. 2783915460 \boxed{2783915460}

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...