# Lexicographic Permutations: Euler Problem 24

(This article was first published on The Devil is in the Data, and kindly contributed to R-bloggers)

Euler Problem 24 asks to develop lexicographic permutations which are ordered arrangements of objects in lexicographic order. Tushar Roy of Coding Made Simple has shared a great introduction on how to generate lexicographic permutations.

## Euler Problem 24 Definition

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 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

What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?

## Brute Force Solution

The digits 0 to 9 have permutations (including combinations that start with 0). Most of these permutations are, however, not in lexicographic order. A brute-force way to solve the problem is to determine the next lexicographic permutation of a number string and repeat this one million times.

```nextPerm  1 && a[i - 1] >= a[i])
i
This code takes the following steps:
Find largest index $i$ such that $a_{i-1} < a_i$.
If no such index exists, then this is already the last permutation.

Find largest index $j$ such that $j geq i$ and $a_j > a_{i-1}$.
Swap $a_j$ and $a_{i-1}$.
Reverse the suffix starting at $a_i$.
Combinatorics
A more efficient solution is to use combinatorics, thanks to MathBlog. The last nine digits can be ordered in $9! = 362880$ ways. So the first $9!$ permutations start with a 0. By extending this thought, it follows that the millionth permutation must start with a 2.
$lfloor (1000000 - 1) / 9! rfloor = 2$
From this rule, it follows that the 725761st permutation is 2013456789. We now need 274239 more lexicographic permutations:
$(1000000 - 1) - (2 times 9!) = 274239$
We can repeat this logic to find the next digit. The last 8 digits can be ordered in 40320 ways. The second digit is the 6th digit in the remaining numbers, which is 7 (2013456789).
$lfloor 274239 / 8! rfloor = 6$
$274239 - (6 times 7!) = 32319$
This process is repeated until all digits have been used.
```
```numbers
R blogger Tony's Bubble Universe created a generalised function to solve this problem a few years ago.
The post Lexicographic Permutations: Euler Problem 24 appeared first on The Devil is in the Data.

To leave a comment for the author, please follow the link and comment on their blog:  The Devil is in the Data.
R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

```

Source:: R News