# A persistent difference

Here's a fun math problem. I wasn't able to get it - am curious what you guys have to say.

Pick a four-digit number whose digits are not all the same. From its digits form the smallest four-digit number $m$ and the largest, $M$. Find $(M-m)$. Keep repeating the procedure. (Treat, say $234$ as $0234$.) Eventually you will reach $6,174$ - permamently, since:

$7,641-1,467=6,174.$

For example, start with $4,818$:

$8,841-1,488=7,353;$

$7,533-3,357=4,176;$

$7,641-1,467=6,174;$ and so on.

Can you prove that you always reach $6,174$?

This is known as Kaprekar's constant. We can partition the set of possible four digit combinations in to those with the same difference. Note that because $M$ and $m$ share the same digits, they are congruent modulo $9$ and so their difference is a multiple of $9$, and so the number of partitions we need to consider is smaller than one might imagine.
In fact, the set of four-digit integers which can be written as $M-m$ is only on the order of $50$. So, these partitions can then be fairly easily enumerated and placed in to a diagram much like the one below found on Wikipedia. It's a rather 'brute force' approach, but it works.