# Kaprekar number

In mathematics, a natural number in a given number base is a $p$-Kaprekar number if the representation of its square in that base can be split into two parts, where the second part has $p$ digits, that add up to the original number. For example, in base 10, 45 is a 2-Kaprekar number, because 45² = 2025, and 20 + 25 = 45. The numbers are named after D. R. Kaprekar.

## Definition and properties

Let $n$ be a natural number. Then the Kaprekar function for base $b>1$ and power $p>0$ $F_{p,b}:\mathbb {N} \rightarrow \mathbb {N}$ is defined to be the following:
    $F_{p,b}(n)=\alpha +\beta$,
where $\beta =n^{2}{\bmod {b}}^{p}$ and
    $\alpha ={\frac {n^{2}-\beta }{b^{p}}}$
A natural number $n$ is a $p$-Kaprekar number if it is a fixed point for $F_{p,b}$, which occurs if $F_{p,b}(n)=n$. $0$ and $1$ are trivial Kaprekar numbers for all $b$ and $p$, all other Kaprekar numbers are nontrivial Kaprekar numbers.
The earlier example of 45 satisfies this definition with $b=10$ and $p=2$, because
    $\beta =n^{2}{\bmod {b}}^{p}=45^{2}{\bmod {1}}0^{2}=25$
    $\alpha ={\frac {n^{2}-\beta }{b^{p}}}={\frac {45^{2}-25}{10^{2}}}=20$
    $F_{2,10}(45)=\alpha +\beta =20+25=45$
A natural number $n$ is a sociable Kaprekar number if it is a periodic point for $F_{p,b}$, where $F_{p,b}^{k}(n)=n$ for a positive integer $k$ (where $F_{p,b}^{k}$ is the $k$th iterate of $F_{p,b}$), and forms a cycle of period $k$. A Kaprekar number is a sociable Kaprekar number with $k=1$, and a amicable Kaprekar number is a sociable Kaprekar number with $k=2$.
The number of iterations $i$ needed for $F_{p,b}^{i}(n)$ to reach a fixed point is the Kaprekar function's persistence of $n$, and undefined if it never reaches a fixed point.
There are only a finite number of $p$-Kaprekar numbers and cycles for a given base $b$, because if $n=b^{p}+m$, where $m>0$ then
    ${\begin{aligned}n^{2}&=(b^{p}+m)^{2}\\&=b^{2p}+2mb^{p}+m^{2}\\&=(b^{p}+2m)b^{p}+m^{2}\\\end{aligned}}$
and $\beta =m^{2}$, $\alpha =b^{p}+2m$, and $F_{p,b}(n)=b^{p}+2m+m^{2}=n+(m^{2}+m)>n$. Only when $n\leq b^{p}$ do Kaprekar numbers and cycles exist.
If $d$ is any divisor of $p$, then $n$ is also a $p$-Kaprekar number for base $b^{p}$.
In base $b=2$, all even perfect numbers are Kaprekar numbers. More generally, any numbers of the form $2^{n}(2^{n+1}-1)$ or $2^{n}(2^{n+1}+1)$ for natural number $n$ are Kaprekar numbers in base 2.

### Set-theoretic definition and unitary divisors

The set $K(N)$ for a given integer $N$ can be defined as the set of integers $X$ for which there exist natural numbers $A$ and $B$ satisfying the Diophantine equation
    $X^{2}=AN+B$, where $0\leq B<N$
    $X=A+B$
An $n$-Kaprekar number for base $b$ is then one which lies in the set $K(b^{n})$.
It was shown in 2000 that there is a bijection between the unitary divisors of $N-1$ and the set $K(N)$ defined above. Let $\operatorname {Inv} (a,c)$ denote the multiplicative inverse of $a$ modulo $c$, namely the least positive integer $m$ such that $am=1{\bmod {c}}$, and for each unitary divisor $d$ of $N-1$ let $e={\frac {N-1}{d}}$ and $\zeta (d)=d\ {\text{Inv}}(d,e)$. Then the function $\zeta$ is a bijection from the set of unitary divisors of $N-1$ onto the set $K(N)$. In particular, a number $X$ is in the set $K(N)$ if and only if $X=d\ {\text{Inv}}(d,e)$ for some unitary divisor $d$ of $N-1$.
The numbers in $K(N)$ occur in complementary pairs, $X$ and $N-X$. If $d$ is a unitary divisor of $N-1$ then so is $e={\frac {N-1}{d}}$, and if $X=d\operatorname {Inv} (d,e)$ then $N-X=e\operatorname {Inv} (e,d)$.

## Kaprekar numbers for $F_{p,b}$
### b = 4k + 3 and p = 2n + 1

Let $k$ and $n$ be natural numbers, the number base $b=4k+3=2(2k+1)+1$, and $p=2n+1$. Then:
* $X_{1}={\frac {b^{p}-1}{2}}=(2k+1)\sum _{i=0}^{p-1}b^{i}$ is a Kaprekar number.
Let
${\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,
${\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 $\alpha$ and $\beta$ are
    $\beta =X_{1}^{2}{\bmod {b}}^{p}=k+1+\sum _{i=1}^{n}(kb+(3k+2))b^{2i-1}$
    $\alpha ={\frac {X_{1}^{2}-\beta }{b^{p}}}=k+\sum _{i=1}^{n}(kb+(3k+2))b^{2i-1}$
and their sum is
${\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, $X_{1}$ is a Kaprekar number.
* $X_{2}={\frac {b^{p}+1}{2}}=X_{1}+1$ is a Kaprekar number for all natural numbers $n$.
Let
${\begin{aligned}X_{2}&={\frac {b^{2n+1}+1}{2}}\\&={\frac {b^{2n+1}-1}{2}}+1\\&=X_{1}+1\end{aligned}}$
Then,
${\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 $\alpha$ and $\beta$ are
    $\beta =X_{2}^{2}{\bmod {b}}^{p}=k+1+\sum _{i=1}^{n}(kb+(3k+2))b^{2i-1}$
    $\alpha ={\frac {X_{2}^{2}-\beta }{b^{p}}}=k+1+\sum _{i=1}^{n}(kb+(3k+2))b^{2i-1}$
and their sum is
${\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, $X_{2}$ is a Kaprekar number.

### b = m$^2$k + m + 1 and p = mn + 1

Let $m$, $k$, and $n$ be natural numbers, the number base $b=m^{2}k+m+1$, and the power $p=mn+1$. Then:
* $X_{1}={\frac {b^{p}-1}{m}}=(mk+1)\sum _{i=0}^{p-1}b^{i}$ is a Kaprekar number.
* $X_{2}={\frac {b^{p}+m-1}{m}}=X_{1}+1$ is a Kaprekar number.

### b = m$^2$k + m + 1 and p = mn + m − 1

Let $m$, $k$, and $n$ be natural numbers, the number base $b=m^{2}k+m+1$, and the power $p=mn+m-1$. Then:
* $X_{1}={\frac {m(b^{p}-1)}{4}}=(m-1)(mk+1)\sum _{i=0}^{p-1}b^{i}$ is a Kaprekar number.
* $X_{2}={\frac {mb^{p}+1}{4}}=X_{3}+1$ is a Kaprekar number.

### b = m$^2$k + m$^2$ − m + 1 and p = mn + 1

Let $m$, $k$, and $n$ be natural numbers, the number base $b=m^{2}k+m^{2}-m+1$, and the power $p=mn+m-1$. Then:
* $X_{1}={\frac {(m-1)(b^{p}-1)}{m}}=(m-1)(mk+1)\sum _{i=0}^{p-1}b^{i}$ is a Kaprekar number.
* $X_{2}={\frac {(m-1)b^{p}+1}{m}}=X_{1}+1$ is a Kaprekar number.

### b = m$^2$k + m$^2$ − m + 1 and p = mn + m − 1

Let $m$, $k$, and $n$ be natural numbers, the number base $b=m^{2}k+m^{2}-m+1$, and the power $p=mn+m-1$. Then:
* $X_{1}={\frac {b^{p}-1}{m}}=(mk+1)\sum _{i=0}^{p-1}b^{i}$ is a Kaprekar number.
* $X_{2}={\frac {b^{p}+m-1}{m}}=X_{3}+1$ is a Kaprekar number.

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

All numbers are in base $b$.
| Base $b$ | Power $p$ | Nontrivial Kaprekar numbers $n\neq 0$, $n\neq 1$ | Cycles |
| 2 | 1 | 10 | $\varnothing$ |
| 3 | 1 | 2, 10 | $\varnothing$ |
| 4 | 1 | 3, 10 | $\varnothing$ |
| 5 | 1 | 4, 5, 10 | $\varnothing$ |
| 6 | 1 | 5, 6, 10 | $\varnothing$ |
| 7 | 1 | 3, 4, 6, 10 | $\varnothing$ |
| 8 | 1 | 7, 10 | 2 → 4 → 2 |
| 9 | 1 | 8, 10 | $\varnothing$ |
| 10 | 1 | 9, 10 | $\varnothing$ |
| 11 | 1 | 5, 6, A, 10 | $\varnothing$ |
| 12 | 1 | B, 10 | $\varnothing$ |
| 13 | 1 | 4, 9, C, 10 | $\varnothing$ |
| 14 | 1 | D, 10 | $\varnothing$ |
| 15 | 1 | 7, 8, E, 10 | 2 → 4 → 2 9 → B → 9 |
| 16 | 1 | 6, A, F, 10 | $\varnothing$ |
| 2 | 2 | 11 | $\varnothing$ |
| 3 | 2 | 22, 100 | $\varnothing$ |
| 4 | 2 | 12, 22, 33, 100 | $\varnothing$ |
| 5 | 2 | 14, 31, 44, 100 | $\varnothing$ |
| 6 | 2 | 23, 33, 55, 100 | 15 → 24 → 15 41 → 50 → 41 |
| 7 | 2 | 22, 45, 66, 100 | $\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 | $\varnothing$ |
| 3 | 4 | 121, 2102, 2222, 10000 | $\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 | $\varnothing$ |
| 3 | 8 | 2012021, 10121020, 12101210, 21121001, 20210202, 22222222, 100000000 | $\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.