Kaprekar’s 6174

A number trick that always lands on 6174 — checked for every case, with one honest caveat.

kaprekar.js · output · proof · check · try it in the playground


The question

Take a four-digit number whose digits are not all the same. Arrange its digits from high to low and from low to high, and subtract the smaller number from the larger. Repeat with the result.

3524  ->  5432 - 2345 = 3087
3087  ->  8730 - 0378 = 8352
8352  ->  8532 - 2358 = 6174

Once you reach 6174 you stay there: 7641 − 1467 = 6174. The mathematician D. R. Kaprekar noticed that every starting number ends up at 6174.

Does every such number get there within seven steps?


Historical background

D. R. Kaprekar described the special role of 6174 in four-digit rearrangement and subtraction. Keeping leading zeros is part of the routine the example tests. Each subtraction can be treated as a step on a finite set of digit strings and checked exhaustively. University of St Andrews, D. R. Kaprekar


A shortcut that makes it checkable

There are 10,000 four-digit strings (0000 to 9999, leading zeros allowed), but the first step only cares about which digits occur, not their order: 3524 and 2453 give the same result.

So it is enough to check each multiset of four digits (a bag of digits, order ignored) once. Leaving out the ten where all digits are equal (0000, 1111, …), there are 705 of them.


What we tell eyemind

One step of the routine, written as a backward rule:

// One step: the largest number the digits make minus the smallest.
implied_by(
  kaprekar_step(A, B),
  [
    digits(A, Ds),
    sort4(Ds, Asc),
    reverse(Asc, Desc),
    number_of(Asc, Low),
    number_of(Desc, High),
    is(B, sub(High, Low)),
  ],
)

implied_by(conclusion, conditions) is a backward rule: the conclusion holds when all the conditions in the list hold, and eyemind uses it only when it needs it. Capitalized names are variables, placeholders for values. Read it as: one step takes A to B if A’s digits, sorted low to high, make the number Low, sorted high to low make High, and B is High − Low. Other rules, not shown, split a number into its four digits, sort them, turn digits back into a number, and list one representative number per multiset.

And the claim to test:

// reaches_6174(A, Steps): A was reached after Steps steps, and 6174 follows
// before the total number of steps exceeds seven.
fact(reaches_6174(6174, _))
implied_by(
  reaches_6174(A, Steps),
  [
    ne(A, 6174),
    lt(Steps, 7),
    kaprekar_step(A, B),
    is(Next, add(Steps, 1)),
    reaches_6174(B, Next),
  ],
)

// The claim holds when no multiset is a counterexample; not(...) checks
// every one.
implied_by('counterexample', [digit_multiset(A), not(reaches_6174(A, 0))])
implied_by(kaprekar_verified(6174, 7), not('counterexample'))

not(...) means “cannot be shown”. To decide it, eyemind tries to show the thing inside and concludes the opposite if it fails. That works here because the search is finite: 705 multisets, at most seven steps each.


What eyemind concludes

// Answers: each instance of a query() that holds.
kaprekar_verified(6174, 7)

The comment line says what follows: the answers to the program’s question. eyemind searched all 705 multisets and found no counterexample, so the claim holds. --stats reports 152,940 inferences for that search.


How “there is no counterexample” ends up in a proof

A proof normally shows how something was found: this rule, these facts, this calculation. But “there is no counterexample” is about something that was not found. There is nothing to point at.

eyemind backs such a conclusion with call tables. A table called(call, answers) lists every answer a call gives: for the negation, called('counterexample', []) says the search for a counterexample gave none. The search behind it made many more calls, one per multiset and per Kaprekar step, and each gets its own table. The checker never redoes the search. It checks each table by expanding its call one level against the program, as a search would, and answering every inner call from that call’s own table, so no table vouches for itself. If every table agrees with its expansion, the absence is checked, not taken on trust.


The proof, line by line

This is the proof, with its claim and the start of its certificate; the certificate has 13,556 call tables, of which three are shown:

// Answers: each instance of a query() that holds.
kaprekar_verified(6174, 7)

// Certificate: the derived facts and every answer of each call the questions make.
called(kaprekar_verified(6174, 7), [kaprekar_verified(6174, 7)])
called('counterexample', [])
// … the table for digit_multiset(A): the 705 representative numbers
called(in_range(0, 9, B), [in_range(0, 9, 0), in_range(0, 9, 1), in_range(0, 9, 2), in_range(0, 9, 3), in_range(0, 9, 4), in_range(0, 9, 5), in_range(0, 9, 6), in_range(0, 9, 7), in_range(0, 9, 8), in_range(0, 9, 9)])
// … 13552 more call tables: every Kaprekar step the multisets take

It has two parts:

There are no steps. Steps record the firings of forward rules, and this program has none: every rule is a backward rule, so every call is answered by its table, and the tables are checked against the rules directly.

Read the first lines as sentences:

  1. The question kaprekar_verified(6174, 7) has exactly one answer: the claim.
  2. There is no counterexample: the call 'counterexample' has no answers.
  3. The digit multisets are exactly the 705 numbers listed.
  4. Counting from 0 to 9 gives 0, 1, …, 9, used to build those multisets.

The whole exhaustive search sits behind the second line: all 705 multisets and every Kaprekar step they take. The certificate records what each call answered, including the calls that answered nothing, so that the search can be checked too.


Checked: what the report says

eyemind -c generates the proof and checks it against the program; --check-proof checks a saved proof. The checker never reasons by itself: it reads the clauses from the program and confirms the proof against them in one procedure:

This is the complete report:

// Checked answers: these follow from the input program and are every answer of its questions.
kaprekar_verified(6174, 7)

verdict('checked')

“There is no counterexample” is true relative to this program, that is, as far as these rules and facts can show. The verdict is checked: the claim follows from the program, and it is every answer of the question. The checker did not re-run the search: it confirmed the record of it, table by table.


Checking a search without redoing it

The claim is a claim about absence: no number fails. A checker could redo the whole search, or take it on trust. eyemind’s checker does neither: it checks the written record of the search, one call at a time, and never searches itself. The record is large, 13,556 tables, but each table is a small check: one level of search, with every inner call looked up.


Try it

eyemind examples/kaprekar.js            # the answers
eyemind --proof examples/kaprekar.js    # answers with their proof
eyemind --goal "kaprekar_step(3524, B)" examples/kaprekar.js

Or open it in the playground. The --goal line asks for one step of the routine: it answers kaprekar_step(3524, 3087), the first line of the example above.


Takeaway

eyemind checked a famous number fact case by case, and its call tables turn “we searched and found nothing” into something a checker can confirm.