Project Euler : The Millionth Lexicographic Permutation Of The Digits

The 24th problem of Project Euler wanted the one-millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:

012   021   102   120   201   210

This was yet another problem which I solved with paper and pencil.

We determine one digit at a time. Say the most significant digit is x, then the remaining 9 digits can fill up the remaining places of the 10 digit number. This can be done in 9! = 362880 ways.

362880 X 2 < 1000000 < 362880 X 3

This implies that all the numbers with 0 and 1 as the most significant digits make up 362880 X 2 numbers, which is less than a million, and when we start with 2 as the most significant digit, the one millionth number comes before the series is over. Thus our first digit is 2.

Now with that fixed, we need to find the 1000000 – 2 X 362880 = 274240th number arranged lexicographically using digits 0, 1, 3, 4, 5, 6, 7, 8 and 9. This time we need to consider 8! = 40320.

40320 X 6 < 274240 < 40320 X 7

Thus we need the 7th digit which is 7. So our number starts with 27.

The same step is to be repeated over and over again. In a condensed notation, the digits found are:

9! X 2 < 1000000 < 9! X 3 => digit is 2; 1000000 – 725760 = 274240

8! X 6 < 27420 < 8! X 7 => digit is 7; 27420 – 241920 = 32320

7! X 6 < 32320 < 7! X 7 => digit is 8; 32320 – 30240 = 2080

6! X 2 < 2080 < 6! X 3 => digit is 3; 2080 – 1440 = 640

5! X 5 < 640 < 5! X 6 => digit is 9; 640 – 600 = 40

4! X 1 < 40 < 4! X 2 => digit is 1; 40 – 24 = 16

3! X 2 < 16 < 3! X 3 => digit is 5; 16 – 12 = 4

At this point the digits left are 0, 4 and 6. The 4th number ordered lexicographically will be 460.The number we set out to find is thus 2783915460.

Popularity: 41% [?]

Related posts:

  1. Project Euler : Largest N Digit Pandigital Prime Number
  2. Project Euler : Numbers That Are Fifth Powers Of Their Digits
  3. Project Euler : Finding The nth Digit Of The Fractional Part Of Irrational Number
  4. Project Euler : Sum Of Digits In a^b
  5. Project Euler : Largest Product Of Five Consequtive Digits

  • Liza

    I didn’t really get it.. How would you find 10000th or 100000th permutation? I only know the first digit is 0 (because 362800 is already more than 10000 or 100000). And from there I don’t really know where to go.. Can you explain it a little bit more detailed?

  • http://blog.singhanuvrat.com Anuvrat

    Say I have fixed the first digit as d. Now the remaining 9 digits can be arranged in 9! = 362880 ways. This implies that with the first digit as 0 we will have 362880 numbers, which is less than a million. After all the 362880 number have been exhausted, 0 becomes 1. Once again there will be 362880 numbers, bringing the sum to 362880×2 numbers, which still is less than a million. Now 1 becomes 2. And this time, 362880×3 is greater than million. Thus our 10 digit number lies between 2……… and 3………, whereby we conclude that the first digit has to be 2.

    • ColleenComb

      I was wondering how to do the opposite?  If I am given a permutation, how do I find which term the permutation is in in lexiographic order?

      • http://blog.singhanuvrat.com Anuvrat Singh

        That too should be pretty simple right? Say you want to know what the position of 210 is in all 3 digit permutations containing digits 0, 1 and 2. We can reverse the above logic and arrive at the answer.

        Take the most significant digit – 2. This means that we have already exhausted all the numbers starting with 0 and 1 = 2*2! = 4. 
        Now take the next digit – 1. This means that we have exhausted all the number with 20 as the first two digits = 1*1! = 1.
        Next is the last digit. So the position of this number will be after the 4 + 1 numbers = 6th.

        • ColleenComb

          I understand that, but that is the last term in the permutation.  I tried to use your logic for 231 when i am using 1, 2, 3 and writing permutations.  I get 231 as the 6th term in the order in lexiographic order. But, wouldn’t that be 321?  I did 1*1!+2*2!+1*1!=6.  But if I write out the permutations, which is easy to do here, I get 231 as the 4th term.  What is wrong with my reasoning?  HELP!!!!!!

          • http://blog.singhanuvrat.com Anuvrat Singh

            Ok. Let’s work out your example. We need the position of 231.

            Most significant digit = 2. This means that we have already exhausted all the numbers starting with 1 = 1*2! = 2.
            Next digit = 3. This means we have exhausted all the numbers that have 21 as the first two digits = 1*1! = 1.
            Next is the last digit. So the position of this number will be after the 2 + 1 numbers = 4th. :)

            The difference between our cases is that I am using 0, 1, 2 to form the numbers and you are using 1, 2 and 3. So in your computation, it is not 2*2! but actually 1*2!. 

          • colleencomb

            I really want to thank you for your help.  But, could you please explain to me why you are using 1*2! and then 1*1! I just need to understand why, because I have a few other problems, 5,6, and 7 characters in the problem.  I am truly sorry for being dense and frantic.  I am trying to learn this on my own because I had an accident and haven’t been in class for a few weeks.  Trying to figure it out on my own and not get too far behind.  I appreciate anything you can do to help me! THANKS

          • ColleenComb

            I am sorry to bother again. I am still working on this. I took a break for a bit. I have f5(15243). My friend gets 14th term. And I got 21st term. Who is right? Either of us? Could you explain the solution? Thanks. I have exams this week and I am stressing a bit.

  • Liza

    Thanks a lot :) I got it!