In mathematics, a natural number in a given number base is a ${\displaystyle p}$-Kaprekar number if the representation of its square in that base can be split into two parts, where the second part has ${\displaystyle p}$ digits, that add up to the original number. The numbers are named after D. R. Kaprekar.

## Definition and properties

Let ${\displaystyle n}$ be a natural number. We define the Kaprekar function for base ${\displaystyle b>1}$ and power ${\displaystyle p>0}$ ${\displaystyle F_{p,b}:\mathbb {N} \rightarrow \mathbb {N} }$ to be the following:

${\displaystyle F_{p,b}(n)=\alpha +\beta }$,

where ${\displaystyle \beta =n^{2}{\bmod {b))^{p))$ and

${\displaystyle \alpha ={\frac {n^{2}-\beta }{b^{p))))$

A natural number ${\displaystyle n}$ is a ${\displaystyle p}$-Kaprekar number if it is a fixed point for ${\displaystyle F_{p,b))$, which occurs if ${\displaystyle F_{p,b}(n)=n}$. ${\displaystyle 0}$ and ${\displaystyle 1}$ are trivial Kaprekar numbers for all ${\displaystyle b}$ and ${\displaystyle p}$, all other Kaprekar numbers are nontrivial Kaprekar numbers.

For example, in base 10, 45 is a 2-Kaprekar number, because

${\displaystyle \beta =n^{2}{\bmod {b))^{p}=45^{2}{\bmod {1))0^{2}=25}$
${\displaystyle \alpha ={\frac {n^{2}-\beta }{b^{p))}={\frac {45^{2}-25}{10^{2))}=20}$
${\displaystyle F_{2,10}(45)=\alpha +\beta =20+25=45}$

A natural number ${\displaystyle n}$ is a sociable Kaprekar number if it is a periodic point for ${\displaystyle F_{p,b))$, where ${\displaystyle F_{p,b}^{k}(n)=n}$ for a positive integer ${\displaystyle k}$ (where ${\displaystyle F_{p,b}^{k))$ is the ${\displaystyle k}$th iterate of ${\displaystyle F_{p,b))$), and forms a cycle of period ${\displaystyle k}$. A Kaprekar number is a sociable Kaprekar number with ${\displaystyle k=1}$, and a amicable Kaprekar number is a sociable Kaprekar number with ${\displaystyle k=2}$.

The number of iterations ${\displaystyle i}$ needed for ${\displaystyle F_{p,b}^{i}(n)}$ to reach a fixed point is the Kaprekar function's persistence of ${\displaystyle n}$, and undefined if it never reaches a fixed point.

There are only a finite number of ${\displaystyle p}$-Kaprekar numbers and cycles for a given base ${\displaystyle b}$, because if ${\displaystyle n=b^{p}+m}$, where ${\displaystyle m>0}$ then

{\displaystyle {\begin{aligned}n^{2}&=(b^{p}+m)^{2}\\&=b^{2p}+2mb^{p}+m^{2}\\&=(b^{p}+2m)b^{p}+m^{2}\\\end{aligned))}

and ${\displaystyle \beta =m^{2))$, ${\displaystyle \alpha =b^{p}+2m}$, and ${\displaystyle F_{p,b}(n)=b^{p}+2m+m^{2}=n+(m^{2}+m)>n}$. Only when ${\displaystyle n\leq b^{p))$ do Kaprekar numbers and cycles exist.

If ${\displaystyle d}$ is any divisor of ${\displaystyle p}$, then ${\displaystyle n}$ is also a ${\displaystyle p}$-Kaprekar number for base ${\displaystyle b^{p))$.

In base ${\displaystyle b=2}$, all even perfect numbers are Kaprekar numbers. More generally, any numbers of the form ${\displaystyle 2^{n}(2^{n+1}-1)}$ or ${\displaystyle 2^{n}(2^{n+1}+1)}$ for natural number ${\displaystyle n}$ are Kaprekar numbers in base 2.

### Set-theoric definition and unitary divisors

We can define the set ${\displaystyle K(N)}$ for a given integer ${\displaystyle N}$ as the set of integers ${\displaystyle X}$ for which there exist natural numbers ${\displaystyle A}$ and ${\displaystyle B}$ satisfying the Diophantine equation[1]

${\displaystyle X^{2}=AN+B}$, where ${\displaystyle 0\leq B
${\displaystyle X=A+B}$

An ${\displaystyle n}$-Kaprekar number for base ${\displaystyle b}$ is then one which lies in the set ${\displaystyle K(b^{n})}$.

It was shown in 2000[1] that there is a bijection between the unitary divisors of ${\displaystyle N-1}$ and the set ${\displaystyle K(N)}$ defined above. Let ${\displaystyle {\text{Inv))(a,c)}$ denote the multiplicative inverse of ${\displaystyle a}$ modulo ${\displaystyle c}$, namely the least positive integer ${\displaystyle m}$ such that ${\displaystyle am=1{\bmod {c))}$, and for each unitary divisor ${\displaystyle d}$ of ${\displaystyle N-1}$ let ${\displaystyle e={\frac {N-1}{d))}$ and ${\displaystyle \zeta (d)=d\ {\text{Inv))(d,e)}$. Then the function ${\displaystyle \zeta }$ is a bijection from the set of unitary divisors of ${\displaystyle N-1}$ onto the set ${\displaystyle K(N)}$. In particular, a number ${\displaystyle X}$ is in the set ${\displaystyle K(N)}$ if and only if ${\displaystyle X=d\ {\text{Inv))(d,e)}$ for some unitary divisor ${\displaystyle d}$ of ${\displaystyle N-1}$.

The numbers in ${\displaystyle K(N)}$ occur in complementary pairs, ${\displaystyle X}$ and ${\displaystyle N-X}$. If ${\displaystyle d}$ is a unitary divisor of ${\displaystyle N-1}$ then so is ${\displaystyle e={\frac {N-1}{d))}$, and if ${\displaystyle X=d\ {\text{Inv))(d,e)}$ then ${\displaystyle N-X=e\ {\text{Inv))(e,d)}$.

## Kaprekar numbers for ${\displaystyle F_{p,b))$

### b = 4k + 3 and p = 2n + 1

Let ${\displaystyle k}$ and ${\displaystyle n}$ be natural numbers, the number base ${\displaystyle b=4k+3=2(2k+1)+1}$, and ${\displaystyle p=2n+1}$. Then:

• ${\displaystyle X_{1}={\frac {b^{p}-1}{2))=(2k+1)\sum _{i=0}^{p-1}b^{i))$ is a Kaprekar number.
Proof

Let

{\displaystyle {\begin{aligned}X_{1}&={\frac {b^{p}-1}{2))\\&={\frac {b-1}{2))\sum _{i=0}^{p-1}b^{i}\\&={\frac {4k+3-1}{2))\sum _{i=0}^{2n+1-1}b^{i}\\&=(2k+1)\sum _{i=0}^{2n}b^{i}\\\end{aligned))}

Then,

{\displaystyle {\begin{aligned}X_{1}^{2}&=\left({\frac {b^{p}-1}{2))\right)^{2}\\&={\frac {b^{2p}-2b^{p}+1}{4))\\&={\frac {b^{p}(b^{p}-2)+1}{4))\\&={\frac {(4k+3)^{2n+1}(b^{p}-2)+1}{4))\\&={\frac {(4k+3)^{2n}(b^{p}-2)(4k+4)-(4k+3)^{2n}(b^{p}-2)+1}{4))\\&={\frac {-(4k+3)^{2n}(b^{p}-2)+1}{4))+(k+1)(4k+3)^{2n}(b^{p}-2)\\&={\frac {-(4k+3)^{2n-1}(b^{p}-2)(4k+4)+(4k+3)^{2n-1}(b^{p}-2)+1}{4))+(k+1)b^{2n}(b^{2n+1}-2)\\&={\frac {(4k+3)^{2n-1}(b^{p}-2)+1}{4))+(k+1)b^{2n}(b^{p}-2)-(k+1)b^{2n-1}(b^{2n+1}-2)\\&={\frac {(4k+3)^{p-2}(b^{p}-2)+1}{4))+\sum _{i=p-2}^{p-1}(-1)^{i}(k+1)b^{i}(b^{p}-2)\\&={\frac {(4k+3)^{p-2}(b^{p}-2)+1}{4))+(b^{p}-2)(k+1)\sum _{i=p-2}^{p-1}(-1)^{i}b^{i}\\&={\frac {(4k+3)^{1}(b^{p}-2)+1}{4))+(b^{p}-2)(k+1)\sum _{i=1}^{p-1}(-1)^{i}b^{i}\\&={\frac {-(b^{p}-2)+1}{4))+(b^{p}-2)(k+1)\sum _{i=0}^{p-1}(-1)^{i}b^{i}\\&=(b^{p}-2)(k+1)\left(\sum _{i=0}^{2n}(-1)^{i}b^{i}\right)+{\frac {-b^{2n+1}+3}{4))\\&=(b^{p}-2)(k+1)\left(\sum _{i=0}^{2n}(-1)^{i}b^{i}\right)+{\frac {-4b^{2n+1}+3b^{2n+1}+3}{4))\\&=(b^{p}-2)(k+1)\left(\sum _{i=0}^{2n}(-1)^{i}b^{i}\right)-b^{p}+{\frac {3b^{2n+1}+3}{4))\\&=(b^{p}-2)(k+1)\left(\sum _{i=0}^{2n}(-1)^{i}b^{i}\right)-b^{p}+{\frac {3(4k+3)^{p-2}+3}{4))+3(k+1)\sum _{i=p-2}^{p-1}(-1)^{i}b^{i}\\&=(b^{p}-2)(k+1)\left(\sum _{i=0}^{2n}(-1)^{i}b^{i}\right)-b^{p}+{\frac {3(4k+3)^{1}+3}{4))+3(k+1)\sum _{i=1}^{p-1}(-1)^{i}b^{i}\\&=(b^{p}-2)(k+1)\left(\sum _{i=0}^{2n}(-1)^{i}b^{i}\right)-b^{p}+{\frac {-3+3}{4))+3(k+1)\sum _{i=0}^{p-1}(-1)^{i}b^{i}\\&=(b^{p}-2)(k+1)\left(\sum _{i=0}^{2n}(-1)^{i}b^{i}\right)+3(k+1)\left(\sum _{i=0}^{2n}(-1)^{i}b^{i}\right)-b^{p}\\&=(b^{p}-2+3)(k+1)\left(\sum _{i=0}^{2n}(-1)^{i}b^{i}\right)-b^{p}\\&=(b^{p}+1)(k+1)\left(\sum _{i=0}^{2n}(-1)^{i}b^{i}\right)-b^{p}\\&=(b^{p}+1)\left(-1+(k+1)\sum _{i=0}^{2n}(-1)^{i}b^{i}\right)+1\\&=(b^{p}+1)\left(k+(k+1)\sum _{i=1}^{2n}(-1)^{i}b^{i}\right)+1\\&=(b^{p}+1)\left(k+(k+1)\sum _{i=1}^{n}b^{2i}-b^{2i-1}\right)+1\\&=(b^{p}+1)\left(k+(k+1)\sum _{i=1}^{n}(b-1)b^{2i-1}\right)+1\\&=(b^{p}+1)\left(k+\sum _{i=1}^{n}((k+1)b-k-1)b^{2i-1}\right)+1\\&=(b^{p}+1)\left(k+\sum _{i=1}^{n}(kb+(4k+3)-k-1)b^{2i-1}\right)+1\\&=(b^{p}+1)\left(k+\sum _{i=1}^{n}(kb+(3k+2))b^{2i-1}\right)+1\\&=b^{p}\left(k+\sum _{i=1}^{n}(kb+(3k+2))b^{2i-1}\right)+\left(k+1+\sum _{i=1}^{n}(kb+(3k+2))b^{2i-1}\right)\\\end{aligned))}

The two numbers ${\displaystyle \alpha }$ and ${\displaystyle \beta }$ are

${\displaystyle \beta =X_{1}^{2}{\bmod {b))^{p}=k+1+\sum _{i=1}^{n}(kb+(3k+2))b^{2i-1))$
${\displaystyle \alpha ={\frac {X_{1}^{2}-\beta }{b^{p))}=k+\sum _{i=1}^{n}(kb+(3k+2))b^{2i-1))$

and their sum is

{\displaystyle {\begin{aligned}\alpha +\beta &=\left(k+\sum _{i=1}^{n}(kb+(3k+2))b^{2i-1}\right)+\left(k+1+\sum _{i=1}^{n}(kb+(3k+2))b^{2i-1}\right)\\&=2k+1+\sum _{i=1}^{n}((2k)b+2(3k+2))b^{2i-1}\\&=2k+1+\sum _{i=1}^{n}((2k)b+(6k+4))b^{2i-1}\\&=2k+1+\sum _{i=1}^{n}((2k)b+(4k+3))b^{2i-1}+(2k+1)b^{2i-1}\\&=2k+1+\sum _{i=1}^{n}((2k+1)b)b^{2i-1}+(2k+1)b^{2i-1}\\&=2k+1+\sum _{i=1}^{n}(2k+1)b^{2i}+(2k+1)b^{2i-1}\\&=2k+1+\sum _{i=1}^{2n}(2k+1)b^{i}\\&=\sum _{i=0}^{2n}(2k+1)b^{i}\\&=(2k+1)\sum _{i=0}^{2n}b^{i}\\&=X_{1}\\\end{aligned))}

Thus, ${\displaystyle X_{1))$ is a Kaprekar number.

• ${\displaystyle X_{2}={\frac {b^{p}+1}{2))=X_{1}+1}$ is a Kaprekar number for all natural numbers ${\displaystyle n}$.
Proof

Let

{\displaystyle {\begin{aligned}X_{2}&={\frac {b^{2n+1}+1}{2))\\&={\frac {b^{2n+1}-1}{2))+1\\&=X_{1}+1\\\end{aligned))}

Then,

{\displaystyle {\begin{aligned}X_{2}^{2}&=(X_{1}+1)^{2}\\&=X_{1}^{2}+2X_{1}+1\\&=X_{1}^{2}+2X_{1}+1\\&=b^{p}\left(k+\sum _{i=1}^{n}(kb+(3k+2))b^{2i-1}\right)+\left(k+1+\sum _{i=1}^{n}(kb+(3k+2))b^{2i-1}\right)+b^{p}-1+1\\&=b^{p}\left(k+1+\sum _{i=1}^{n}(kb+(3k+2))b^{2i-1}\right)+\left(k+1+\sum _{i=1}^{n}(kb+(3k+2))b^{2i-1}\right)\\\end{aligned))}

The two numbers ${\displaystyle \alpha }$ and ${\displaystyle \beta }$ are

${\displaystyle \beta =X_{2}^{2}{\bmod {b))^{p}=k+1+\sum _{i=1}^{n}(kb+(3k+2))b^{2i-1))$
${\displaystyle \alpha ={\frac {X_{2}^{2}-\beta }{b^{p))}=k+1+\sum _{i=1}^{n}(kb+(3k+2))b^{2i-1))$

and their sum is

{\displaystyle {\begin{aligned}\alpha +\beta &=\left(k+1+\sum _{i=1}^{n}(kb+(3k+2))b^{2i-1}\right)+\left(k+1+\sum _{i=1}^{n}(kb+(3k+2))b^{2i-1}\right)\\&=2k+2+\sum _{i=1}^{n}((2k)b+2(3k+2))b^{2i-1}\\&=2k+2+\sum _{i=1}^{n}((2k)b+(6k+4))b^{2i-1}\\&=2k+2+\sum _{i=1}^{n}((2k)b+(4k+3))b^{2i-1}+(2k+1)b^{2i-1}\\&=2k+2+\sum _{i=1}^{n}((2k+1)b)b^{2i-1}+(2k+1)b^{2i-1}\\&=2k+2+\sum _{i=1}^{n}(2k+1)b^{2i}+(2k+1)b^{2i-1}\\&=2k+2+\sum _{i=1}^{2n}(2k+1)b^{i}\\&=1+\sum _{i=0}^{2n}(2k+1)b^{i}\\&=1+(2k+1)\sum _{i=0}^{2n}b^{i}\\&=1+X_{1}\\&=X_{2}\\\end{aligned))}

Thus, ${\displaystyle X_{2))$ is a Kaprekar number.

### b = m2k + m + 1 and p = mn + 1

Let ${\displaystyle m}$, ${\displaystyle k}$, and ${\displaystyle n}$ be natural numbers, the number base ${\displaystyle b=m^{2}k+m+1}$, and the power ${\displaystyle p=mn+1}$. Then:

• ${\displaystyle X_{1}={\frac {b^{p}-1}{m))=(mk+1)\sum _{i=0}^{p-1}b^{i))$ is a Kaprekar number.
• ${\displaystyle X_{2}={\frac {b^{p}+m-1}{m))=X_{1}+1}$ is a Kaprekar number.

### b = m2k + m + 1 and p = mn + m - 1

Let ${\displaystyle m}$, ${\displaystyle k}$, and ${\displaystyle n}$ be natural numbers, the number base ${\displaystyle b=m^{2}k+m+1}$, and the power ${\displaystyle p=mn+m-1}$. Then:

• ${\displaystyle X_{1}={\frac {m(b^{p}-1)}{4))=(m-1)(mk+1)\sum _{i=0}^{p-1}b^{i))$ is a Kaprekar number.
• ${\displaystyle X_{2}={\frac {mb^{p}+1}{4))=X_{3}+1}$ is a Kaprekar number.

### b = m2k + m2 - m + 1 and p = mn + 1

Let ${\displaystyle m}$, ${\displaystyle k}$, and ${\displaystyle n}$ be natural numbers, the number base ${\displaystyle b=m^{2}k+m^{2}-m+1}$, and the power ${\displaystyle p=mn+m-1}$. Then:

• ${\displaystyle X_{1}={\frac {(m-1)(b^{p}-1)}{m))=(m-1)(mk+1)\sum _{i=0}^{p-1}b^{i))$ is a Kaprekar number.
• ${\displaystyle X_{2}={\frac {(m-1)b^{p}+1}{m))=X_{1}+1}$ is a Kaprekar number.

### b = m2k + m2 - m + 1 and p = mn + m - 1

Let ${\displaystyle m}$, ${\displaystyle k}$, and ${\displaystyle n}$ be natural numbers, the number base ${\displaystyle b=m^{2}k+m^{2}-m+1}$, and the power ${\displaystyle p=mn+m-1}$. Then:

• ${\displaystyle X_{1}={\frac {b^{p}-1}{m))=(mk+1)\sum _{i=0}^{p-1}b^{i))$ is a Kaprekar number.
• ${\displaystyle X_{2}={\frac {b^{p}+m-1}{m))=X_{3}+1}$ is a Kaprekar number.

## Kaprekar numbers and cycles of ${\displaystyle F_{p,b))$ for specific ${\displaystyle p}$, ${\displaystyle b}$

All numbers are in base ${\displaystyle b}$.

Base ${\displaystyle b}$ Power ${\displaystyle p}$ Nontrivial Kaprekar numbers ${\displaystyle n\neq 0}$, ${\displaystyle n\neq 1}$ Cycles
2 1 10 ${\displaystyle \varnothing }$
3 1 2, 10 ${\displaystyle \varnothing }$
4 1 3, 10 ${\displaystyle \varnothing }$
5 1 4, 5, 10 ${\displaystyle \varnothing }$
6 1 5, 6, 10 ${\displaystyle \varnothing }$
7 1 3, 4, 6, 10 ${\displaystyle \varnothing }$
8 1 7, 10 2 → 4 → 2
9 1 8, 10 ${\displaystyle \varnothing }$
10 1 9, 10 ${\displaystyle \varnothing }$
11 1 5, 6, A, 10 ${\displaystyle \varnothing }$
12 1 B, 10 ${\displaystyle \varnothing }$
13 1 4, 9, C, 10 ${\displaystyle \varnothing }$
14 1 D, 10 ${\displaystyle \varnothing }$
15 1 7, 8, E, 10

2 → 4 → 2

9 → B → 9

16 1 6, A, F, 10 ${\displaystyle \varnothing }$
2 2 11 ${\displaystyle \varnothing }$
3 2 22, 100 ${\displaystyle \varnothing }$
4 2 12, 22, 33, 100 ${\displaystyle \varnothing }$
5 2 14, 31, 44, 100 ${\displaystyle \varnothing }$
6 2 23, 33, 55, 100

15 → 24 → 15

41 → 50 → 41

7 2 22, 45, 66, 100 ${\displaystyle \varnothing }$
8 2 34, 44, 77, 100

4 → 20 → 4

11 → 22 → 11

45 → 56 → 45

2 3 111, 1000 10 → 100 → 10
3 3 111, 112, 222, 1000 10 → 100 → 10
2 4 110, 1010, 1111, 10000 ${\displaystyle \varnothing }$
3 4 121, 2102, 2222, 10000 ${\displaystyle \varnothing }$
2 5 11111, 100000

10 → 100 → 10000 → 1000 → 10

111 → 10010 → 1110 → 1010 → 111

3 5 11111, 22222, 100000 10 → 100 → 10000 → 1000 → 10
2 6 11100, 100100, 111111, 1000000

100 → 10000 → 100

1001 → 10010 → 1001

100101 → 101110 → 100101

3 6 10220, 20021, 101010, 121220, 202202, 212010, 222222, 1000000

100 → 10000 → 100

122012 → 201212 → 122012

2 7 1111111, 10000000

10 → 100 → 10000 → 10

1000 → 1000000 → 100000 → 1000

100110 → 101111 → 110010 → 1010111 → 1001100 → 111101 → 100110

3 7 1111111, 1111112, 2222222, 10000000

10 → 100 → 10000 → 10

1000 → 1000000 → 100000 → 1000

1111121 → 1111211 → 1121111 → 1111121

2 8 1010101, 1111000, 10001000, 10101011, 11001101, 11111111, 100000000 ${\displaystyle \varnothing }$
3 8 2012021, 10121020, 12101210, 21121001, 20210202, 22222222, 100000000 ${\displaystyle \varnothing }$
2 9 10010011, 101101101, 111111111, 1000000000

10 → 100 → 10000 → 100000000 → 10000000 → 100000 → 10

1000 → 1000000 → 1000

10011010 → 11010010 → 10011010

## Extension to negative integers

Kaprekar numbers can be extended to the negative integers by use of a signed-digit representation to represent each integer.

## Programming exercise

The example below implements the Kaprekar function described in the definition above to search for Kaprekar numbers and cycles in Python.

def kaprekarf(x: int, p: int, b: int) -> int:
beta = pow(x, 2) % pow(b, p)
alpha = (pow(x, 2) - beta) // pow(b, p)
y = alpha + beta
return y

def kaprekarf_cycle(x: int, p: int, b: int) -> List[int]:
seen = []
while x < pow(b, p) and x not in seen:
seen.append(x)
x = kaprekarf(x, p, b)
if x > pow(b, p):
return []
cycle = []
while x not in cycle:
cycle.append(x)
x = kaprekarf(x, p, b)
return cycle