This example explores Kaprekar’s routine for four-digit numbers. The routine sorts a number’s digits descending and ascending, subtracts the smaller arrangement from the larger one, and repeats.
For many four-digit starts, this process reaches 6174, known as Kaprekar’s constant.
Every start is treated as four digits, so 1 becomes 0001. The program applies the Kaprekar step repeatedly, tracking chains that reach 6174 within the configured bound. Starts with all identical digits, such as 1111, fall to 0000 instead and are omitted from the emitted Kaprekar facts.
The output reports 9,990 emitted starts, 10 omitted starts, and a maximum of 7 steps to reach 6174. It also prints selected chains and a distribution showing how many starts take 0, 1, 2, …, 7 steps.
The trust gate verifies the emitted and omitted counts, the maximum step count, selected known chains, and the step-count distribution. This protects the digit-padding, subtraction, and bounded-search logic.
From the repository root:
node examples/kaprekar_6174.js