Four-Digit Kaprekar Dynamics in Odd Bases

Evan Chen and Ken Ono

Axiom Math 124 University Avenue Palo Alto, CA 94301

USA

evan@axiommath.ai
ken@axiommath.ai

Richard E. Schwartz^{1} Department of Mathematics

Brown University

Box 1917 151 Thayer Street Providence, RI 02912

USA

res@math.brown.edu

Dinesh Thakur Department of Mathematics

1013 Hylan Building University of Rochester Rochester, NY 14627

USA

dinesh.thakur@rochester.edu

1Richard E. Schwartz’s research is supported by NSF grant DMS-2505281.

Abstract Start with four digits, arrange them in both descending and ascending order, subtract, and repeat. This simple process is known as the Kaprekar routine, famous in base ten for sending every nonconstant four-digit string to 6174. We show that in every odd base B > 3, the four-digit Kaprekar map has an unexpectedly rigid structure. After at most three iterations, every nonconstant orbit enters an explicit triangular region and on this region the map is conjugate to projective doubling:

{[r], [s]} ⟼ {[2r], [2s]}.

This gives a complete finite description of all nonconstant terminal cycles, including an explicit formula for their lengths and counts. In particular, the longest terminal cycle has length at most (B − 1)/2, and equality can occur only when B is prime. For primes p > 5, equality occurs precisely when the least positive m with 2^{m} ≡ ±1 (mod

p) is m = (p − 1)/2. The results proved here were first formulated by Schwartz and Thakur. As a test case for AI-assisted formal mathematics, AxiomProver produced Lean/mathlib formalizations of these results.

1 Introduction and main results

1.1 Background

Kaprekar’s four-digit routine looks, at first, like a numerical magic trick. Take four digits, arrange them in decreasing order, subtract the same digits in increasing order, and repeat. In base ten, the trick is famously dramatic: the process is drawn to the constant 6174, which is a fixed point since

7641 − 1467 = 6174.

For example, starting with the number 2808, we have the process

2808 ⟼ 8532 ⟼ 6174 ⟼ 6174.

The surprise is not merely that a fixed point appears, but that a rule made from sorting digits is secretly governed by modular arithmetic. This paper explains that hidden arithmetic for four digits in every odd base.

Kaprekar introduced this routine in his study of decimal digit phenomena [5, 6, 2]. In the classical four-digit decimal case, every nonconstant digit string reaches the fixed point 6174. For 3 decimal digits, the fixed point is 495. The decimal case, however, is only one member of a larger family. In other bases the four-digit routine need not have a single attracting fixed point. Its terminal behavior may consist of several cycles, and their lengths depend on the arithmetic of the base.

The generalized Kaprekar problem has been studied from several points of view. In base 10, fixed points of the Kaprekar map (for every digit length) define the integer sequence OEIS \underline{A099009}. Jordan studied self-producing digit sequences [4]. Hasse and Prichett determined the four-digit Kaprekar constants (i.e., fixed points for general bases) [3]. Prichett analyzed five-digit terminating cycles [11]. Thakur developed a broader framework for Kaprekar phenomena in arbitrary bases and digit lengths [12]. Devlin and Zeng studied maximum distances to the four-digit fixed point in the bases where such a fixed point exists [1].

More recently, Yamagami and Matsui developed a detailed theory of 3-adic Kaprekar loops and obtained formulas for Kaprekar fixed points [13, 14]. Kay and Downes-Ward extended fixed-point and cycle questions to odd bases using Kaprekar indices and subgroup/coset methods, and their sequel treats even bases, where the classification is more intricate [7, 8]. Our purpose here is more explicit. In the four-digit odd-base case, the right coordinates reveal a particularly simple mechanism: after a bounded pre-periodic segment, the Kaprekar map is just projective doubling. The resulting proof is short, but the simplification is quite striking, and gives a transparent route to the cycle-length consequences below while keeping the digit dynamics visible throughout.

1.2 Main classification result

Given a 4-digit positive integer in an odd base B, let a_{1} ≥ a_{2} ≥ a_{3} ≥ a_{4} denote its digits in non-increasing order. As noted by Hasse and Prichett [3], the next Kaprekar iterate depends only on the two differences

d_{1} = a_{1} − a_{4} and d_{2} = a_{2} − a_{3}.  (1)

Thus d_{1} records the outer difference and d_{2} records the inner difference. Accordingly, we define a natural state space

X_{B} := {(d_{1}, d_{2}) : 1 ≤ d_{1} < B, 0 ≤ d_{2} ≤ d_{1}}.  (2)

The Kaprekar routine thus induces a map K_{B}: X_{B} → X_{B}, sending each difference pair to the next difference pair. Note here we have excluded the trivial case a_{1} = a_{2} = a_{3} = a_{4} by requiring d_{1} ≠ 0.

In Section 2 we prove that, after a short pre-periodic segment, these two differences become positive, unequal, and odd; that is, (d_{1}, d_{2}) will lie in the stable odd region defined by

T_{B} := {(d_{1}, d_{2}) ∈ X_{B} : d_{1} > d_{2} > 0 and d_{1} ≡ d_{2} ≡ 1 (mod 2)}.  (3)

We also describe how K_{B} acts on T_{B}. To give this description, rather than working with d_{1} and d_{2} themselves, we work with their half-sum and half-difference:

r = \frac{d_{1} + d_{2}}{2}  and s = \frac{d_{1} − d_{2}}{2} .  (4)

(Note that r > s > 0, and that r and s are integers because d_{1} and d_{2} are odd.)

For pairs (r, s) we pass to projective residue classes modulo B, where each residue is identified with its negative. Introduce the notation

P_{B} := ((ℤ/Bℤ) \ {0})/{±1}

for the nonzero projective residue classes. For nonzero x ∈ ℤ/Bℤ, let [x] := {x, −x} ∈ P_{B} denote the corresponding residue class in P_{B}. Finally, let \binom{P_{B}}{2} denote the (unordered) twoelement subsets of P_{B}.

We can now state the main structural result.

Theorem 1 (Structural classification). Let B > 3 be odd, and let K_{B}: X_{B} → X_{B} be the four-digit Kaprekar map in difference coordinates. Then T_{B}, as defined in (3), is forwardinvariant and

K_{B}^{3}(X_{B}) ⊆ T_{B}.

Moreover, the restricted map K_{B}: T_{B} → T_{B} is conjugate to the doubling map

\binom{P_{B}}{2} → \binom{P_{B}}{2} by \{[r], [s]\} ⟼ \{[2r], [2s]\}

under the transformation defined in (4).

Here, in the context of finite dynamical systems, we explicate the term “conjugate” as follows. Let f: X → X and g: Y → Y be maps. We say that f and g are conjugate under the bijection Φ: X → Y if

Φ(f(x)) = g(Φ(x)) for all x ∈ X.

Thus Φ is a relabeling of the states which turns one system into the other. In the particular case of Theorem 1, the map Φ: T_{B} → \binom{P_{B}}{2} is given by (4), and so Theorem 1 makes an implicit promise that (4) is bijective.

We can thus describe Theorem 1 in English as follows: After at most three iterations, every orbit enters our smaller stable region T_{B}. And on this region T_{B}, the sorting-and-subtracting rule is no longer mysterious! After the change of variables in (4), it is exactly multiplication by 2, with signs ignored, on unordered pairs of nonzero projective residue classes.

Remark 2. Here the case B = 3 needs to be excluded because T_{B} is empty when B = 3.

1.3 Cycle lengths

Clearly, for odd B the doubling map

\binom{P_{B}}{2} → \binom{P_{B}}{2} by \{[r], [s]\} ⟼ \{[2r], [2s]\}

is bijective. Since conjugate systems have the same cycle lengths, we get the following corollaries as the main consequences. They are stated separately so that the dynamical statement and the arithmetic consequences are easy to distinguish.

Corollary 3 (Cycle-length bound). If B > 3 is odd and c_{max}(B) is the largest terminal-cycle length for the four-digit Kaprekar map in base B, then we have

c_{max}(B) ≤ \frac{B − 1}{2}.

Moreover, equality holds if and only if B ≥ 7, B is prime, and the least positive solution to 2^{m} ≡ ±1 (mod B) is m = \frac{B−1}{2}.

Remark 4. In the edge case B = 5, one has c_{max}(5) = 1 < 2 = (B − 1)/2 instead (see [3, Lemma 1], or [7, §4 and Appendix B]).

Thus Corollary 3 gives a universal ceiling and describes exactly when this bound is achieved. We also have the following result:

Corollary 5 (Prime bases and maximal cycles). If equality holds in Corollary 3 (so that B is prime), then the number of terminal cycles of length c_{max}(B) is

⌊\frac{c_{max}(B) − 1}{2}⌋ .

Remark 6. Our results are a four-digit complement to the broader odd-base theory. Yamagami and Matsui give a detailed theory in base 3 [13], and Kay and Downes-Ward study fixed points and cycles in odd bases using Kaprekar indices, with regular cycles organized through subgroup and coset data [7]. Their framework treats arbitrary digit-counts and is especially well suited to regular cycles. The present note fixes the digit-count at four. In this small case, the dynamics are more directly described by the outer and inner digit differences, and after a bounded pre-periodic segment these two differences give a stable region on which the map becomes projective doubling. Therefore, the contribution here is a low-dimensional model that makes the four-digit odd-base dynamics and their cycle counts explicit.

1.4 Provenance and formal verification

The structural conjectures leading to Theorem 1 and its cycle-length consequences were first formulated by Schwartz and Thakur and communicated to Ono. AxiomProver was then used as an experimental AI-assisted formal mathematics system to produce Lean/mathlib formalizations of the resulting statements. The present paper gives a human-readable account of the mathematics. The formal files, the Lean environment, and the verification protocol are described in Section 6. AxiomProver is credited as a tool rather than as an author, and the human authors take responsibility for the mathematical statements, exposition, attribution, and correctness of the paper.

1.5 Examples of the main results

Here are a few examples that illustrate these results. The cycles are controlled not by decimal accidents, but by the order of 2 modulo the base, with signs ignored. We start each example with ordinary four-digit strings, and only then pass to the difference and projective coordinates.

Example 7 (The case B = 7; [7, §5]). The sorted four-digit string 3100 has

d_{1} = 3 − 0 = 3 and d_{2} = 1 − 0 = 1.

Thus

r = \frac{3 + 1}{2} = 2 and s = \frac{3 − 1}{2} = 1.

Writing each iterate as a sorted digit string, the orbit of 3100 is

3100 ⟼ 5430 ⟼ 5520 ⟼ 5322 ⟼ 5430 ⟼ …,

which in difference coordinates is the terminal cycle

(3, 1) ⟼ (5, 1) ⟼ (5, 3) ⟼ (3, 1).

Here (B −1)/2 = 3, and 2 has projective order 3 modulo 7, so the largest four-digit Kaprekar cycles have length 3. This agrees with the count ⌊(3 − 1)/2⌋ = 1.

Example 8 (The case B = 11). The sorted four-digit string 5100 has (d_{1}, d_{2}) = (5, 1), and hence

r = 3 and s = 2.

Writing each iterate as a sorted digit string, the orbit of 5100 is

5100 ⟼ 9650 ⟼ 9920 ⟼ 9632 ⟼ 7742 ⟼ 7652 ⟼ 9650 ⟼ ….

In difference coordinates this is the terminal cycle

(5, 1) → (9, 1) → (9, 7) → (7, 3) → (5, 3) → (5, 1).

Since 2^{5} ≡ −1 (mod 11) and no smaller positive power of 2 is congruent to ±1, we have c_{max}(11) = 5. There are

⌊\frac{5 − 1}{2}⌋ = 2

cycles of length five; the other one is

(3, 1) → (9, 5) → (7, 1) → (9, 3) → (7, 5) → (3, 1).

Example 9 (The case B = 9). The composite base B = 9 shows why the inequality is usually strict. Starting from the sorted string 5100, we have (d_{1}, d_{2}) = (5, 1) and (r, s) = (3, 2). The terminal behavior in difference coordinates is

(5, 1) → (7, 1) → (7, 5) → (5, 1),

with the second cycle

(3, 1) → (7, 3) → (5, 3) → (3, 1).

Here (B − 1)/2 = 4, but 2^{3} ≡ −1 (mod 9), so the terminal cycles have length 3, not 4.

Example 10 (The case B = 17). Finally, a prime base need not give equality. For B = 17, the sorted four-digit string 5100 again has (d_{1}, d_{2}) = (5, 1) and (r, s) = (3, 2), but 2^{4} ≡ −1 (mod 17). Thus the largest terminal cycles have length 4, not (17 − 1)/2 = 8.

The rest of the paper is organized as follows. Section 2 introduces the difference coordinates and proves that every orbit reaches the stable odd region after at most three steps. Section 3 identifies the hidden projective coordinates and proves that, on this region, one Kaprekar step is just doubling in those coordinates. Section 4 translates the projective model into explicit cycle-count formulas, and then deduces the general cycle-length bound and the prime-base obstruction to equality. Section 5 treats prime bases in detail and counts the largest cycles when the bound is attained. Section 6 describes the accompanying Lean/mathlib formalization, including the AxiomProver protocol, the formal files, and the verification environment.

2 Difference coordinates for four digits

We now turn from the statement of the result to the mechanism behind it. The first step is to reduce the digit operation to a map on two integers.

2.1 Difference coordinates

Fix an odd base B > 3. Let

\overrightarrow{N} = [a_{1}, a_{2}, a_{3}, a_{4}] with a_{1} ≥ a_{2} ≥ a_{3} ≥ a_{4}

be the decreasing rearrangement of a four-digit string. We ignore constant strings, since they map to zero. As in (1), let d_{1} := a_{1} − a_{4} and d_{2} := a_{2} − a_{3} so that

1 ≤ d_{1} < B and 0 ≤ d_{2} ≤ d_{1}.

Then Kaprekar subtraction depends only on (d_{1}, d_{2}), as in [3]. Indeed,

\begin{matrix}(a_{1}B^{3} + a_{2}B^{2} + a_{3}B + a_{4}) − (a_{4}B^{3} + a_{3}B^{2} + a_{2}B + a_{1}) \\ = d_{1}(B^{3} − 1) + d_{2}(B^{2} − B). \end{matrix}

Thus the future orbit after one step is determined by the pair (d_{1}, d_{2}).

If d_{2} > 0, ordinary base-B subtraction gives the unsorted output digits

[d_{1}, d_{2} − 1, B − d_{2} − 1, B − d_{1}].  (5)

If d_{2} = 0, the subtraction has one more borrow, and the unsorted output digits are

[d_{1} − 1, B − 1, B − 1, B − d_{1}].  (6)

Let K_{B} denote the induced map on X_{B} as defined in (2). To compute K_{B}(d_{1}, d_{2}), one sorts the four entries in (5) or (6) in decreasing order and then takes the outer and inner differences. This passage to difference coordinates does not lose eventual cycle lengths. Indeed, a digit-level orbit determines a difference-coordinate orbit, and conversely a periodic orbit in difference coordinates lifts to a digit-level periodic orbit by the subtraction formulas (5) and (6). If two digit strings in such a lifted orbit were equal, their difference pairs would also be equal, so the period is the same. Thus it is enough to count terminal cycles for K_{B} on X_{B}.

2.2 Escaping the pre-period

The rest of this section is a cleanup step. We show that the boundary phenomena disappear after a bounded number of iterations, leaving a simple stable region on which the projective model will live.

Lemma 11 (Pre-period into the stable odd region). For every odd B > 3,

K_{B}^{3}(X_{B}) ⊆ T_{B}.

Moreover T_{B} is forward invariant under K_{B}.

The proof of Lemma 11 is divided into a few cases resolved via explicit calculation of K_{B}. This calculation also appears in [3, equations (1.1)–(1.3)], and [1, §2], although our organization of the cases is slightly different. Case 11.1. Lemma 11 holds in the case d_{1} > d_{2} > 0. In fact:

• If d_{1} > d_{2} > 0 and d_{1} ≡ d_{2} (mod 2), then K_{B}(d_{1}, d_{2}) ∈ T_{B}.

• If d_{1} > d_{2} > 0 but d_{1} ≢ d_{2} (mod 2), then K_{B}^{2}(d_{1}, d_{2}) ∈ T_{B}.

Proof for Case 11.1. Abbreviate the four entries in (5) by

A := d_{1} and C := d_{2} − 1 and D := B − d_{2} − 1 and E := B − d_{1}.

We always have A > C and D ≥ E; also A ≠ E and C ≠ D. We note the case D > A > C > E is also impossible, because this case would require both d_{1} + d_{2} < B − 1 and d_{1} + d_{2} > B + 1. Hence, we must be in one of the five situations listed in Table 1. Of course, only rows whose displayed differences are nonnegative can actually occur.

We assert the last four rows of Table 1 always lie in T_{B}. Indeed, it is clear the entries are odd. Moreover, they are easily seen to be unequal; in the third row we have C ≥ E ⟹ d_{1} + d_{2} ≥ B + 1 while in the fourth row we have D ≥ A ⟹ d_{1} + d_{2} ≤ B − 1. Hence in the third and fourth row d_{1} + d_{2} ≠ B always holds and the two entries are unequal.

It remains to only deal with the first row, which lies in T_{B} exactly when d_{1} ≡ d_{2} (mod 2). It suffices to show that

K_{B}(h + 1, h − 1) ∈ T_{B} for each odd h ≥ 1.  (7)

Indeed (7) can be checked directly:

Ordering  K_{B}(d_{1}, d_{2})  In T_{B}?
A ≥ D ≥ E ≥ C (d_{1} − d_{2} + 1, d_{1} − d_{2} − 1) Iff d_{1} ≡ d_{2} mod 2
D ≥ E > A > C  (B − 2d_{2}, B − 2d_{1})  Always
A ≥ D > C ≥ E  (2d_{1} − B, B − 2d_{2})  Always
D ≥ A > E ≥ C  (B − 2d_{2}, 2d_{1} − B)  Always
A > C > D ≥ E  (2d_{1} − B, 2d_{2} − B)  Always
Table 1: Possible results of K_{B}(d_{1}, d_{2}) when d_{1} > d_{2} > 0.

• If h ≠ 1, we apply Table 1 a second time to the new pair (h+1, h−1). For (h+1, h−1), all five rows of Table 1 are in T_{B} and we are done.

• If h = 1 then in fact (h + 1, h − 1) = (2, 0). Although Table 1 does not apply, we can

directly compute

K_{B}(2, 0) = (B − 2, 1) ∈ T_{B}.

Case 11.2. We have K_{B}^{2}(d, d) ∈ T_{B} for every d > 0.

Proof for Case 11.2. Formula (5) becomes

[d, d − 1, B − d − 1, B − d].

Then

K_{B}(d, d) = (h + 1, h − 1) where h := |B − 2d|.

This is the same as (7) from the earlier case, so we get K_{B}(h + 1, h − 1) ∈ T_{B} as desired.

Case 11.3. We have K_{B}^{3}(d_{1}, 0) ∈ T_{B} for every d_{1} > 0.

Proof for Case 11.3. Put

u = d_{1} − 1 and v = B − d_{1}.

Sorting (6) gives

K_{B}(d_{1}, 0) = (B − 1 − min(u, v), B − 1 − max(u, v)).  (8)

If the second coordinate in (8) is zero, then d_{1} = 1 and

(1, 0) ↦ (B − 1, 0) ↦ (B − 2, 1) ∈ T_{B}.

If the image in (8) has positive unequal coordinates, then Case 11.1 applies. If the image has equal coordinates, then Case 11.2 applies. Hence every state with d_{2} = 0 reaches T_{B} within at most three steps.

We can now deduce the main lemma.

Proof of Lemma 11. We note that K_{B}(T_{B}) ⊆ T_{B} follows from the first half of Case 11.1, while K_{B}^{3}(X_{B}) ⊆ T_{B} follows by noting that Cases 11.1 to 11.3 cover all possibilities.

Example 12 (The pre-periodic segment in base 11). Let B = 11. An example for Case 11.2 is

(3, 3) ⟼ (6, 4) ⟼ (3, 1) ∈ T_{11}.

An example for Case 11.3 is

(6, 0) ⟼ (5, 5) ⟼ (2, 0) ⟼ (9, 1) ∈ T_{11}.

These two computations illustrate why a short pre-periodic segment is needed before the simple projective model appears.

Remark 13. Lemma 11 is analogous to Hasse-Prichett’s [3, Lemma 3.1], which defines the analog of T_{B} for B = 5 · 2^{n}. In our case of odd B we are able to enforce a uniform bound of 3 on the number of applications of K_{B} needed. But when B = 5 · 2^{n} the number of steps needed to reach a cycle can grow linearly in n (see Devlin-Zeng [1, Theorem 1.1]).

3 The projective model

The previous section deals with the pre-periodic segment. We now reveal the hidden coordinates on the stable region. They are not the differences themselves, but the half-sum and half-difference of the two differences, viewed modulo sign.

Recall that P_{B} = ((ℤ/Bℤ) \ {0})/{±1}. Define

\begin{matrix}Φ: T_{B} & → \binom{P_{B}}{2} \\ by (d_{1}, d_{2}) & ↦ \{[\frac{d_{1} + d_{2}}{2} ] , [\frac{d_{1} − d_{2}}{2} ]\} . \end{matrix}  (9)

The two classes are nonzero and distinct, because d_{1} > d_{2} > 0 and d_{1} < B. We first prove this is a bijection.

Lemma 14 (Bijection with projective pairs). The map Φ: T_{B} → \binom{P_{B}}{2} is a bijection.

Proof. Define the intermediate set

R := {(r, s) | r > s > 0, r + s < B, r ≢ s mod 2}.

Recall the notation from (4). We first contend that the map

T_{B} → R by (d_{1}, d_{2}) ↦ (r, s) = (\frac{d_{1} + d_{2}}{2} , \frac{d_{1} − d_{2}}{2} )  (10)

is a (well-defined) bijection, with inverse given by d_{1} = r + s and d_{2} = r − s. Indeed, the conditions on T_{B} and R line up exactly:

d_{1}, d_{2} odd ⟺ r ± s odd
d_{1} > d_{2} ⟺ s > 0
d_{2} > 0 ⟺ r > s
B > d_{1} ⟺ r + s < B.

This establishes (10) is indeed a bijection. So, it remains to prove that the projection

R → \binom{P_{B}}{2} by (r, s) ↦ \{[r], [s]\}  (11)

is also a bijection. To that end, suppose \{[u], [v]\} ∈ \binom{P_{B}}{2} is an arbitrary pair of distinct projective classes, with 0 < u < B and 0 < v < B. Since [u] ≠ [v] we also have u ≠ v and u + v ≠ B.

The main point is that among the four ordered pairs (u, v), (u, B − v), (B − u, v), (B − u, B − v) it is easy to see exactly one ordered pair has odd sum less than B. (Indeed, exactly two pairs have odd sum, and the sum of all four numbers is 2B.)

So without loss of generality, assume (by renaming u to B − u or v to B − v if needed) that u + v < B is odd. In that case, r = max(u, v) and s = min(u, v) is the unique pair in R with image {[u], [v]}. Hence (11) is also a bijection.

Lemma 14 now follows by composing (10) and (11).

Example 15 (The bijection in base 11). In base 11, consider the two classes [2] = [9] and [5] = [6]. The unique (r, s) pair is r = 5 and s = 2, which gives d_{1} = 7 and d_{2} = 3. Hence the bijection in this case is

Φ(7, 3) = {[5], [2]}.

The next proposition is the heart of the paper. This proposition says that, after the preperiodic segment, the digit dynamics are exactly multiplication by 2 on the two projective coordinates.

Proposition 16 (Projective doubling). For every (d_{1}, d_{2}) ∈ T_{B},

Φ(K_{B}(d_{1}, d_{2})) = \{[2 · \frac{d_{1} + d_{2}}{2} ] , [2 · \frac{d_{1} − d_{2}}{2} ]\} .  (12)

Equivalently, under Φ, the Kaprekar map on T_{B} is conjugate to

{[r], [s]} ⟼ {[2r], [2s]}

on unordered projective pairs.

Ordering  K_{B}(r + s, r − s)  Half-sum mod B Half-diff mod B
A ≥ D ≥ E ≥ C  (2s + 1, 2s − 1)  2s  1
D ≥ E > A > C (B − 2r + 2s, B − 2r − 2s)  −2r  2s
A ≥ D > C ≥ E (2r + 2s − B, B − 2r + 2s)  2s  2r
D ≥ A > E ≥ C (B − 2r + 2s, 2r + 2s − B)  2s  −2r
A > C > D ≥ E (2r + 2s − B, 2r − 2s − B)  2r  2s

Table 2: Computing K_{B}(r + s, r − s) in terms of r and s instead.

Proof. We reuse the earlier Table 1 from the proof of Case 11.1 (which is applicable because we are in T_{B}), but rewrite it in (r, s)-coordinates. That is, we calculate K_{B}(d_{1}, d_{2}) = K_{B}(r + s, r − s) in terms of r and s, then compute the half-sum and half-difference of the new pair, modulo B. This transforms Table 1 into Table 2 below.

In Table 2, we find that in every row except the first, the two projective classes are exactly [2r] and [2s], in some order. This means that (12) is proved in every case except A ≥ D ≥ E ≥ C.

In the problematic case, we then note that A ≥ D and E ≥ C rearrange to

A ≥ D ⟺ d_{1} ≥ B − d_{2} − 1 ⟺ 2r ≥ B − 1
E ≥ C ⟺ B − d_{1} ≥ d_{2} − 1 ⟺ 2r ≤ B + 1.

So in fact this case can only occur when 2r = B − 1 or 2r = B + 1. In either case we have [2r] = [1] in P_{B} and (12) is still true.

Remark 17. One may also simplify the casework by noting that every pair in T_{B} lies in the case treated by [3, equation (1.1)] and [1, §2, “type (a)”], which show that K_{B}(d_{1}, d_{2}) is {|2d_{1} − B|, |2d_{2} − B|} in some order. (In T_{B}, d_{1} + d_{2} is even and hence never equals B.) This makes Proposition 16 follow more directly, eliminating the need to compute Table 2.

Example 18 (The conjugacy in base 11). Start from (d_{1}, d_{2}) = (5, 1) ∈ T_{11}. Then

r = \frac{5 + 1}{2} = 3 and s = \frac{5 − 1}{2} = 2,

so Φ(5, 1) = {[3], [2]}. Doubling gives

{[3], [2]} ⟼ {[6], [4]} = {[5], [4]}

in P_{11}, since [6] = [−5] = [5]. The Kaprekar step gives

(5, 1) ⟼ (9, 1).

For this new state,

Φ(9, 1) = \{[\frac{10}{2}] , [\frac{8}{2}]\} = \{[5], [4]\},

exactly as Proposition 16 predicts.

At this point Theorem 1 is immediate.

Proof of Theorem 1. By Lemma 11, every orbit reaches the forward-invariant region T_{B} after at most three steps in difference coordinates. By Lemma 14, Φ is a bijection from this region to the unordered two-element subsets of P_{B}. By Proposition 16, this bijection carries one Kaprekar step to one doubling step. This proves both the pre-periodic statement and the asserted conjugacy on the stable region.

4 Cycle lengths

We have now replaced the Kaprekar map by projective doubling. The rest of the argument is arithmetic. The key quantity is the order of 2 after signs are ignored (see Yamagami-Matsui [13]; denoted σ(r) in Kay-Downes-Ward [7], and OEIS \underline{A003558}).

Definition 19 (Projective order of 2). For odd B > 3, define

λ(B) = min{m > 0 : 2^{m} ≡ ±1 (mod B)}.  (13)

Equivalently, λ(B) is the order of the class of 2 in the finite group

(ℤ/Bℤ)^{×}/{±1}.

This definition is tailored to P_{B}: multiplying by 2^{λ(B)} brings every projective class back to itself. Consequently the induced map on unordered pairs cannot have longer cycles.

Lemma 20. Every cycle of the doubling map on \binom{P_{B}}{2} has length at most λ(B).

Proof. By definition, 2^{λ(B)} ≡ ±1 (mod B). Therefore multiplication by 2^{λ(B)} fixes every projective class [x] ∈ P_{B}, including classes represented by non-units. Hence the induced action on unordered two-element subsets of P_{B} is also fixed by the λ(B)-th iterate. Every orbit length is therefore at most λ(B).

Lemma 21 ([7, Theorem 2]). Let φ denote the Euler totient function. One has

λ(B) ≤ \frac{φ(B)}{2}.

Proof. The class of 2 lies in the finite group (ℤ/Bℤ)^{×}/{±1}. This group has order φ(B)/2, because B is odd and B > 1. The order of an element in a finite group divides the order of the group. Hence λ(B) ≤ φ(B)/2.

This lets us prove part of Corollary 3 now.

Corollary 22. If B > 3 is odd, then c_{max}(B) ≤ \frac{B−1}{2}. Moreover, if equality holds then B must be prime.

Proof. By Theorem 1, every terminal Kaprekar cycle is represented by a cycle of the doubling map on \binom{P_{B}}{2}. Lemmas 20 and 21 give

c_{max}(B) ≤ λ(B) ≤ \frac{φ(B)}{2} ≤ \frac{B − 1}{2}.

This proves the stated bound.

As for the equality case, suppose that c_{max}(B) = (B − 1)/2. Then every inequality in

c_{max}(B) ≤ λ(B) ≤ \frac{φ(B)}{2} ≤ \frac{B − 1}{2}

must be an equality. In particular, φ(B) = B − 1. For an integer B > 1, this is equivalent to B being prime. Hence equality forces prime base.

This corollary does not say that every prime base is extremal. It only says that composite bases are ruled out. The examples below show both a composite base where the bound is strict and a prime base where the bound is sharp.

Example 23 (The order bound in bases 9 and 11). For B = 9, the projective order of 2 is 3, since 2^{3} ≡ −1 (mod 9). Lemma 20 therefore forces every terminal four-digit cycle in base 9 to have length at most 3, and the examples in the introduction show that this bound is sharp in base 9.

For B = 11, the projective order is 5, since 2^{5} ≡ −1 (mod 11) and no smaller positive exponent gives ±1. Thus the order bound permits cycles of length 5, and the two length-five cycles displayed in the introduction attain it.

The same projective model also gives an explicit count of all terminal cycles. We record this as a proposition because it is the most useful practical form of the classification. For each odd divisor n > 1 of B, put

λ(n) = min{m > 0 : 2^{m} ≡ ±1 (mod n)}.

Following [7, equation (5)], for a positive integer a, define

μ_{B}(a) = ∑_{\begin{matrix}n|B, n>1 \\ λ(n)=a \end{matrix}} \frac{φ(n)}{2a}.  (14)

Thus μ_{B}(a) is the number of a-cycles of the doubling map on P_{B} itself, before passing to unordered pairs.

Theorem 24 (Explicit cycle counts). Let N_{B}(L) be the number of terminal four-digit Kaprekar cycles of length L in the odd base B > 3, with constant digit strings omitted. Then

\begin{aligned}N_{B}(L) = & μ_{B}(L) ⌊\frac{L − 1}{2}⌋ + μ_{B}(2L) + \binom{μ_{B}(L)}{2}L \\ & + ∑_{\begin{matrix}a<b \\ lcm(a,b)=L \end{matrix}} μ_{B}(a)μ_{B}(b) gcd(a, b), \end{aligned}  (15)

where μ_{B}(a) is given by (14), and where μ_{B}(a) = 0 if no divisor n contributes to (14).

Proof. We first compute the cycle structure of doubling on P_{B}; this goes along the lines of Kay-Downes-Ward [8, §2.2]. Fix a nonzero residue class [x] ∈ P_{B}, and let g = gcd(x, B). Write B = gn. Then x = gy with y a unit modulo n. Multiplication by 2 preserves g, and the projective classes with this fixed value of g are naturally identified with

(ℤ/nℤ)^{×}/{±1}.

On this set, multiplication by 2 has order λ(n). Hence all cycles in this stratum have length λ(n), and the number of such cycles is

\frac{|(ℤ/nℤ)^{×}/\{±1\}|}{λ(n)} = \frac{φ(n)}{2λ(n)}.

Summing over all divisors n > 1 of B gives (14).

It remains to pass from cycles on P_{B} to cycles on unordered two-element subsets of P_{B}. This is elementary cycle combinatorics. Suppose first that the two elements are chosen from two distinct cycles of lengths a and b. If a < b, then each choice of the two cycles contributes gcd(a, b) orbits of unordered pairs, each of length lcm(a, b). This gives the final sum in (15). If the two distinct cycles have the same length L, then each unordered pair of such cycles contributes L orbits of length L, giving the term \binom{μ_{B}(L)}{2}L.

Finally suppose that both elements lie in the same a-cycle. Label that cycle cyclically. An unordered pair is determined, up to rotation, by its cyclic distance

1, 2, …, ⌊\frac{a}{2}⌋ .

For each distance smaller than a/2, the orbit has length a. This contributes

μ_{B}(a) ⌊\frac{a − 1}{2}⌋

orbits of length a. If a is even, the antipodal distance a/2 gives one additional orbit of length a/2 for each a-cycle; this accounts for the term μ_{B}(2L) in the count of length-L orbits.

By Theorem 1, terminal Kaprekar cycles are exactly these cycles of the doubling action on \binom{P_{B}}{2}. This proves the formula.

Example 25 (Theorem 24 for B = 9). We give a specific numerical example of Theorem 24 to make (15) more concrete.

Consider B = 9. The divisors of B greater than 1 are n = 3, with λ(3) = 1, and n = 9, with λ(9) = 3. Hence, (14) for B = 9 becomes

μ_{9}(a) = \begin{cases}1, & if a = 1 or a = 3; \\ 0, & otherwise. \end{cases}

Turning to (15), we find N_{9}(L) = 0 unless L = 3. (In the sum on the second line of (15), the only nonzero term is (a, b) = (1, 3).) Thus, we have (noting μ_{9}(6) = 0)

N_{9}(3) = μ_{9}(3) · ⌊\frac{3 − 1}{2}⌋ + \binom{μ_{9}(3)}{2} · 3 + μ_{9}(1)μ_{9}(3) gcd(1, 3)

= 1 · 1 + \binom{1}{2} · 3 + 1 · 1 · 1 = 2.

Hence,

N_{9}(L) = \begin{cases}2, & if L = 3; \\ 0, & otherwise. \end{cases}

In other words, for B = 9, the terminal cycles consist of only two 3-cycles. The cycles turn out to be the following (sorted):

7630 ↦ 7522 ↦ 5542 ↦ 7630
7540 ↦ 7720 ↦ 7432 ↦ 7540.

Example 26 (Theorem 24 for B = 63). Here is a second, more complicated example, with B = 63. In this case, we have λ(3) = 1, λ(7) = λ(9) = 3, and λ(21) = λ(63) = 6. Thus, a direct calculation gives

μ_{63}(a) = \begin{cases}1, & if a = 1; \\ 2, & if a = 3; \\ 4, & if a = 6; \\ 0, & otherwise. \end{cases}

Looking at (15), the first line vanishes unless L ∈ {3, 6}. For the second line, the possible pairs (a, b) with nonzero contribution are (a, b) ∈ {(1, 3), (1, 6), (3, 6)}, corresponding to L = 3, L = 6, L = 6. Thus,

N_{63}(3) = μ_{63}(3) · ⌊\frac{3 − 1}{2}⌋ + μ_{63}(6) + \binom{μ_{63}(3)}{2} · 3 + μ_{63}(1) · μ_{63}(3) · gcd(1, 3)

= 2 · 1 + 4 + \binom{2}{2} · 3 + 1 · 2 · 1 = 11.

N_{63}(6) = μ_{63}(6) · ⌊\frac{6 − 1}{2}⌋ + \binom{μ_{63}(6)}{2} · 6

+ (μ_{63}(1) · μ_{63}(6) · gcd(1, 6) + μ_{63}(3) · μ_{63}(6) · gcd(3, 6))

= 4 · 2 + \binom{4}{2} · 6 + (1 · 4 · 1 + 2 · 4 · 3) = 72.

In summary,

N_{63}(L) = \begin{cases}11, & if L = 3; \\ 72, & if L = 6; \\ 0, & otherwise. \end{cases}

Corollary 27. The following are true.

(i) For odd B > 17, there are at least two terminating λ(B)-cycles. (ii) There is a unique terminating cycle for odd B if and only if B = 3, B = 5 or B = 7. (In other words, these are the only odd bases where all the non-constant strings have the same eventual behaviour.)

Proof. Put L = λ(B). Then (14) implies that μ_{B}(L) ≥ 1. If B > 17 = 2^{4} +1, it is immediate from the definition of λ that L > 4, and thus the calculation of the first term of the right side (15) implies the claim that N_{B}(L) > 1, as all the other terms are non-negative. This implies (i).

For (ii), put N = |P_{B}| = (B − 1)/2, so that \binom{P_{B}}{2} has N(N − 1)/2 elements, while by Lemmas 20 and 21 every cycle of the doubling map has length at most λ(B) ≤ φ(B)/2 ≤ N. If B ≥ 9 then N ≥ 4, hence N(N − 1)/2 > N and the doubling action has at least two orbits. By Theorem 1, there are at least two terminating cycles. Conversely \binom{P_{5}}{2} has a single element, and for B = 7 doubling permutes the three elements of \binom{P_{7}}{2} in one 3-cycle. Finally T_{3} = ∅, so B = 3 lies outside Theorem 1; a direct check shows that all five states of X_{3} enter the cycle (2, 0) ↦ (1, 1) ↦ (2, 0). For B = 5, 7 this cycle is moreover shown to be unanimous in [7]; for B = 3 unanimity follows from the complete classification of [13].

Remark 28. Here the base 5 corresponds to the unique terminating cycle being a 1-cycle (fixed point, i.e., the original Kaprekar phenomena), and that happens [3] for even B, exactly when B = 2^{2n+1} · 5 for n ≥ 0. The numerical evidence suggests that the even bases B for which there is a unique terminating cycle are exactly 2^{2n+1} · 3, 2^{2n+1} · 5, 2^{3n±1} · 7, the cycle lengths being 6(n + 1), 1, and 3 respectively.

5 Prime bases and the number of largest cycles

The preceding section gives the full cycle-count formula and proves the universal bound. In this section we finish the proofs of Corollary 3 and Corollary 5.

5.1 Proof of Corollary 3

Recalling Corollary 22, we suppose B = p > 3 is prime and set q := (p − 1)/2. The simplification is that every nonzero residue is a unit, so P_{p} is a group quotient rather than merely a set of projective residue classes. Thus the projective set P_{p} = 𝔽_{p}^{×}/{±1} has exactly q elements. The doubling map on P_{p} has order

λ(p) = min{m > 0 : 2^{m} ≡ ±1 (mod p)}.

If p > 5, then q ≥ 3. In this case, the induced action on unordered two-element subsets has a cycle of length q if and only if multiplication by 2 is a single q-cycle on P_{p}. Equivalently, this happens if and only if λ(p) = q. Therefore, for primes p > 5,

c_{max}(p) = \frac{p − 1}{2} ⟺ λ(p) = \frac{p − 1}{2}.

This proves the equality criterion in Corollary 3.

The prime p = 5 is exceptional for a simple reason. Here q = 2, so P_{5} has only two elements and \binom{P_{5}}{2} has only one element. Hence the largest induced cycle has length 1, even though λ(5) = 2.

5.2 Proof of Corollary 5

It remains only to count the largest cycles in the equality case. This is a small piece of cycle combinatorics: unordered pairs on a single cycle are classified by their cyclic distance.

Assume p > 5 and λ(p) = q. Then doubling is a single q-cycle on P_{p}. The induced action on unordered two-element subsets of a q-cycle is classified by cyclic distance. The possible distances are

1, 2, …, ⌊\frac{q}{2}⌋ .

For a fixed distance j, all unordered pairs at distance j form one orbit under cyclic rotation. If q is odd, every such orbit has length q. If q is even, the distance q/2 orbit has length q/2, not q, because each pair is antipodal. Therefore the number of orbits of length q is

\begin{cases}(q − 1)/2, & q odd, \\ q/2 − 1, & q even. \end{cases}

Both cases are equal to

⌊\frac{q − 1}{2}⌋ .

Since c_{max} = q, the number of largest terminal Kaprekar cycles is

⌊\frac{c_{max} − 1}{2}⌋ .

This completes the proof of Corollary 5.

Example 29 (Counting largest cycles when p = 11). For p = 11, the projective set P_{11} has q = 5 elements. Since 2 has projective order 5, doubling is a single five-cycle on P_{11}. The unordered pairs of elements of a five-cycle have two possible cyclic distances, namely 1 and 2. These two distances give the two length-five Kaprekar cycles displayed in the introduction. This is the count

⌊\frac{5 − 1}{2}⌋ = 2.

For p = 7, the projective set has q = 3 elements and only one cyclic distance, so there is one largest cycle, again as in the introduction.

6 Appendix: AxiomProver’s autonomous Lean verification

Here we provide the context for this project as well as the protocol used for Lean formalization and verification (see [9, 10]).

6.1 Process

The formal proofs provided in this work were developed and verified using Lean 4.28.0. Compatibility with earlier or later versions is not guaranteed due to the evolving nature of the Lean 4 compiler and its core libraries. The relevant files are all posted in the following repository:

                   https://github.com/AxiomMath/kaprekar4

The input files were:

• kaprekar-input.tex: statements of Theorem 1 and Corollaries 3 and 5 (and the definitions necessary to state these main results)

• task.md: short instructions referring to the Kaprekar input TEX

• .environment: specifies Lean version to use

We note that we did not provide the proofs of the theorems to AxiomProver, only the statements. Based on these input files, AxiomProver generated two output files:

• problem.lean, a Lean 4.28.0 formalization of the problem statement; and

• solution.lean, a complete Lean 4.28.0 formalization of the proof.

With the formal solutions for guidance, the human authors wrote this paper (without the use of AI) for human readers. At first glance, the proofs found by AxiomProver may not resemble the narrative presented in this paper. Turning a Lean file into a human-readable proof is difficult because Lean is written as code for a type-checker.

7 Declaration of generative AI and AI-assisted technologies in the manuscript preparation process

As described in the preceding Appendix, AxiomProver (an AI tool under development) was used to produce a formal proof of Theorem 1 and Corollaries 3 and 5. The paper was written without AI.

8 Acknowledgments

The authors thank Rubén Ballester and Andrew Granville for helpful comments regarding this project, and Granville in particular for suggesting promising directions for future work that generalize these results. We also thank the editors of the Journal of Integer Sequences and the anonymous referee for helpful comments and suggestions, including the suggestion to provide the examples B = 9 and B = 63 in Section 4.

References

[1] P. Devlin and T. Zeng, Maximum distances in the four-digit Kaprekar process, Integers 21 (2021), Paper No. A97.

[2] M. Gardner, Mathematical Games, Scientific American, March 1975,

   p. 112.  Available at https://www.scientificamerican.com/article/
   mathematical-games-1975-03/.

[3] H. Hasse and G. D. Prichett, The determination of all four-digit Kaprekar constants, J. Reine Angew. Math. 299–300 (1978), 113–124.

[4] J. H. Jordan, Self-producing sequences of digits, Amer. Math. Monthly 71 (1964), 61–64.

[5] D. R. Kaprekar, Another solitaire game, Scripta Math. 15 (1949), 244–245.

[6] D. R. Kaprekar, An interesting property of the number 6174, Scripta Math. 21 (1955), 304.

[7] A. Kay and K. Downes-Ward, Fixed points and cycles of the Kaprekar transformation: 1. Odd bases, J. Integer Sequences 25 (2022), Article 22.6.7.

[8] A. Kay and K. Downes-Ward, Fixed points and cycles of the Kaprekar transformation: 2. Even bases. Arxiv preprint arXiv:2408.12257 [math.CO], 2024. Available at https://

   arxiv.org/abs/2408.12257.

[9] The mathlib Community, The Lean mathematical library, In Proceedings of the 9th ACM SIGPLAN International Conference on Certified Programs and Proofs (CPP 2020). ACM, 2020, pp. 367–381.

[10] L. de Moura, S. Kong, J. Avigad, F. van Doorn, and J. von Raumer, The Lean theorem prover (system description), In Automated Deduction – CADE-25, Vol. 9195 of Lecture Notes in Comput. Sci., pp. 378–388. Springer, 2015.

[11] G. D. Prichett, Terminating cycles for iterated difference values of five digit integers, J. Reine Angew. Math. 303–304 (1978), 379–388.

[12] D. S. Thakur, Kaprekar phenomena, Proc. Ropar Conf., Ramanujan Math. Soc. Lect. Notes Ser. 26 (2019), 1–10.

[13] A. Yamagami and Y. Matsui, On 3-adic Kaprekar loops, JP J. Algebra Number Theory Appl. 40 (2018), 957–1028.

[14] A. Yamagami and Y. Matsui, On some formulas for Kaprekar constants, Symmetry 11 (2019), Article 885.

2020 Mathematics Subject Classification: Primary 11A63; Secondary 05A15, 37P99. Keywords: Kaprekar dynamics, digit dynamics, finite dynamical system.

(Concerned with sequences \underline{A003558} and \underline{A099009}.)

Received June 18 2026; revised versions received August 1 2026; August 11 2026. Published in Journal of Integer Sequences, August 12 2026.

Return to Journal of Integer Sequences home page.