Kaprekar's constant

Given a four-digit positive integer \(n\), let \(a\) be the number formed by sorting \(n\)'s digits in ascending order, and let \(d\) be the number formed by sorting the digits in descending order. Discarding any padded zeros, let \(K(n) = d - a\). If we repeat this process starting with a four-digit number with at least two distinct digits, we will always reach Kaprekar's constant 6174, named after discoverer D. R. Kaprekar.