A tricky algorithm

The August Shokunin challenge:

There are a large number of 9 digit integers in the range 123456789 to 987654321 where each digit only appears once.

Your mission should you choose to accept is to identify the 100,000th number in this sequence.

The first number is the easiest to find - 123456789, the second is 123456798, the third is 123456879 and so on. No digit may repeat so 122345675 is not a valid number in this sequence.

Can anyone help me with this.

https://github.com/charleskorn/one-hundred-thousandth-number

How would the code be written?
Last edited on
1
2
3
4
5
8! == 40320, so the 40320th permutation is 213456789
                the 80640th permutation is 312456789

7! == 5040, so the 85680th (80640+5040)th permutation is 321456789
               the 95760th ( 80640 + 5040*3 )th permutation is 351246789


Take it up from there.
(There are only a few thousand permutations left; a lazy option is to brute-force from there on.)
You could use std::next_permutation() defined in the <algorithm> library.

But, this would ruin the point of the challenge so make your own algorithm.
Last edited on
Topic archived. No new replies allowed.