Rule for numbers where each digit differs from the other by $1$

187 Views Asked by At

My question is simple: is there a rule that can tell me when a given numbers has all of its digit differ from the adjacent by $1$?

(I know about the $\mod10$ and shift trick, but I was hoping for a more "direct" formula).

Edit for more clarifications: I'm looking for an equation that could give me a direct result, without iterating through the digits of the number (maybe like a property this set of numbers share, though I fail to see one).

1

There are 1 best solutions below

0
On

If the number is $d_1 d_2 d_3 .. d_n$, it has the property you want if and only if:

$$|(d_1-d_2)(d_2-d_3)...(d_{n-1}-d_n)| = 1$$

(if you ever have two adjacent digits that are the same, then this evaluates to $0$, and if adjacent digits are all different, but not always differ by $1$, then this evaluates to a number greater than $1$)