# Number of $9-$digit numbers, all digits are different and nonzero and having no consecutive digits in consecutive positions

I am trying to solve a problem: We are asked to find the number of $9-$digit numbers by using the digits from $1$ to $9$ only once. There is also one more restriction. Consecutive digits should not be positioned side by side.

For example, $814953627$ is convenient while $8\mathbf{21}574936$ is not.

It seems a hard problem to me. $1$ and $9$ have one consecutive digit while others have two. This makes the problem harder a bit. So I tried to establish a recurrence relation. For $2\leq n\leq 9$, if the number of such numbers is $A_n$, we can use $A_{n-1}$. For each such $(n-1)-$digit numbers, we can find $n-2$ such $n-$digit numbers. Let me give an example, if $n=5$, and we have such a $4-$digit number, $3142$ for instance, we have three position to place digit $5$. Therefore $$A_n = (n-2)A_{n-1} + \cdots$$ Of course this is not the only way to have such an $n-$digit number. We also place the digit $n$ between two consecutive digits. Again if we take $n=5$, and if we have an inconvenient $4-$digit number $4231$, we can place the digit $5$ between $2$ and $3$, and we obtain a convenient $5-$digit number $42531$. However, in this case, we must have only two digits positioned side by side and one of them must not be $n-1$. So, if we say $B_n$ the number of $n-$digit numbers whose digits vary between $1$ and $n$ and it has two and only two consecutive digits in consecutive positions such that consectuive digits are different than $n$, then we have $$A_n = (n-2)A_{n-1} + B_{n-1}.$$ However, we need to have one more recurrence ralation in order to find $B_{n-1}$. I could not find yet. What should I do? Trying to solve by using a completely different method or going over $B_n$? Your helps make me happy for both cases.

The first step of any problem in enumerative combinatorics is to compute the first few cases, then look the sequence up on OEIS. If we do that here, we find A002464:

Hertzsprung's problem: ways to arrange n non-attacking kings on an n X n board, with 1 in each row and column. Also number of permutations of length n without rising or falling successions.

There is no particularly nice formula for $A_n$, which dashes our hopes of finding a particularly nice solution.

Your approach of finding $A_n$ in terms of $A_{n-1}$ and $B_{n-1}$ seems like it might possibly lead to the recurrence $$A_n = (n+1)A_{n-1} - (n-2)A_{n-2} - (n-5)A_{n-3} + (n-3) A_{n-4}$$ after a lot more work. But the original source for this recurrence finds it algebraically, so it's not clear if a combinatorial interpretation is even known.

Taking an inclusion-exclusion type approach to this problem is slightly more straightforward. For this, we want to compute the number of $n$-digit numbers that at least contain some $r$ pairs of adjacent consecutive digits, in $c$ "clumps". For example, in $843256917$, there are $r=3$ adjacent pairs total: $(4,3)$, $(3,2)$, and $(5,6)$, in two clumps: $432$ and $56$.

We can:

• permute the $c$ clumps and $n-r-c$ elements outside the clumps ($n-r$ objects total) in $(n-r)!$ ways,
• orient the clumps in $2^c$ ways,
• choose the sizes of the $c$ clumps (each with size at least $2$, total size $r+c$), in $\binom{r-1}{c-1}$ ways, and
• choose the sizes of the $n-c+1$ gaps between the largest element of one clump and smallest element of the next, plus the start and end (each gap at least $0$, total size of gaps $n-r-c$) in $\binom{n-r}{c}$ ways.

This gives us the formula $$A_n = n! + \sum_{r=1}^{n-1} (-1)^r (n-r)! \sum_{c=1}^r 2^c \binom{r-1}{c-1} \binom{n-r}{c}.$$

(See the Art of Problem Solving thread for more discussion of this formula.)

Finally, we can approximate $A_n$ fairly closely by an asymptotic formula: there are $n-1$ pairs of consecutive elements, which can be adjacent in a random permutation with probability $\frac{2}{n-1}$. Assuming independence (falsely), there is a a $(1 - \frac{2}{n-1})^{n-1} \approx e^{-2}$ chance that no pair of consecutive elements is adjacent, which implies that $A_n \approx e^{-2} n!$.

• Thanks for this nice solution and explanation. – faith Mar 17 '17 at 19:11