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

I ran a simulation a $$1,000,000$$ times since no one found an answer yet.

set.seed(123)
n = 9
samps <- 1000000
good <- 0

for (i in 1:samps){
k <- n
possible <- 1:n
num <- c()
while(k > 0){
x <- sample(possible, 1)
num <- c(num, x)
possible <- possible[-1*which(possible == x)]
k <- k - 1
}
fine <- T
for (j in 1:(n-1)){
if(abs(num[j] - num[j+1]) == 1){
fine = F
}
}
good <- good + fine
}
good/samps*factorial(9)
#48914.05


This is from $$R$$ which is not the best at this but I am pretty sure that the answer is in the $$48,900$$ range.

Edit:

I've programmed the actual organised permutation and the answer accepted is correct.

Here is the code for interest, again written in $$R$$.

library(combinat)
a <- permn(1:9)

good <- 0
for (i in 1:length(a)){
num <- a[[i]]
fine <- T
for (j in 1:(n-1)){
if(abs(num[j] - num[j+1]) == 1){
fine = F
}
}
good <- good + fine
}
good
#47622


There must be a bias in $$R$$ somewhere.

• The OEIS sequence says 47622. – Misha Lavrov Oct 17 '19 at 15:40
• I'm not sure they line up perfectly. The OEIS sequence is something to do with chess. This to me is a lot purer. If there's a problem in my code then I'll take another look though. – stuart stevenson Oct 17 '19 at 15:43
• If you take a $9$-digit number $x_1x_2x_3\dots x_9$ with distinct digits, and put $9$ kinds in the squares $(i,x_i)$ on a $9\times 9$ chessboard, you get one king in each row and column and "two consecutive digits are one apart" is equivalent to "two kings in adjacent columns attack each other" so it is the same problem. – Misha Lavrov Oct 17 '19 at 15:46
• Your own estimate of $9! e^{-2}$ is about $49,100$ which is closer to my answer and in the other direction. I have a hunch that mine is on the right tracks. I can't fault the OEIS value though. It's a nice find. – stuart stevenson Oct 17 '19 at 15:50