If you take any integer and rearrange the digits however you like, the difference between the original number and the rearranged number will always be divisible by 9.

For example:

```
number = 12345
rearranged = 43215
difference = 43215 - 12345 = 30870
difference / 9 = 3430
```

More generally, you can do the same with numbers in any base, and the resulting difference will be divisible by one less than the base.

For example:

```
number in base 8: o12345 (= 5349 in decimal)
rearrange = o43215 (= 18061 in decimal)
difference = o43215 - o12345 = o30650 (= 12712 in decimal)
difference / 7 = 1816 (decimal)
```

We can prove that this holds generally, not just is specially chosen examples.

Let \(b\) be the base of the numbers we're considering, and let \(n\) be the number of digits. Then the original number, \(x\), is defined as

where \(a_i\) are the digits of \(x\).

When we rearrange the digits of \(x\), let \(j(i)\) denote the new position of the digit originally at position \(i\). Then the rearranged number, \(y\), is

Then the difference is

Each term in the series is a constant times \(b^n - b^m\) for some \(n > m\). [Note: if \(i < j(i)\), then the constant is \(-a_i\).]

And \(b-1\) is a factor of \(b^N - 1\) for any natural number \(N\). Thus, \(x-y\) is divisible by \(b-1\), i.e. one less than the base of \(x\) and \(y\).

### Mathematical "Magic"

This is the basis of a mathematical magic trick (the origins of which I don't know). In it, the magician gives the following instructions to the audience:

- Write down a four digit number
- Underneath is, rearrange the digits of the number however you like, to form another four digit number.
- Subtract the smaller number from the larger.
- Circle a non-zero digit of the result.
- Tell me the other digits of the result.

The magician will then tell the audience what the circled digit is.

This works because, as we established, the difference will always be divisible by nine. And thus the sum of the digits will also be a multiple of nine. So the magician needs only to sum up the other digits, and subtract it from the next bigger multiple of nine. (This is why it has to be a non-zero digit that is circled: otherwise you wouldn't be able to tell if it was a zero or a nine.)