Kaprekar number

In mathematics, a natural number in a given number base is a formula_1-Kaprekar number if the representation of its square in that base can be split into two parts, where the second part has formula_1 digits, that add up to the original number. The numbers are named after D. R. Kaprekar. 
Definition and properties.
Let formula_3 be a natural number. We define the Kaprekar function for base formula_4 and power formula_5 formula_6 to be the following:
where formula_8 and 
A natural number formula_3 is a formula_1-Kaprekar number if it is a fixed point for formula_12, which occurs if formula_13. formula_14 and formula_15 are trivial Kaprekar numbers for all formula_16 and formula_1, all other Kaprekar numbers are nontrivial Kaprekar numbers.
For example, in base 10, 45 is a 2-Kaprekar number, because 
A natural number formula_3 is a sociable Kaprekar number if it is a periodic point for formula_12, where formula_23 for a positive integer formula_24 (where formula_25 is the formula_24th iterate of formula_12), and forms a cycle of period formula_24. A Kaprekar number is a sociable Kaprekar number with formula_29, and a amicable Kaprekar number is a sociable Kaprekar number with formula_30.
The number of iterations formula_31 needed for formula_32 to reach a fixed point is the Kaprekar function's persistence of formula_3, and undefined if it never reaches a fixed point.
There are only a finite number of formula_1-Kaprekar numbers and cycles for a given base formula_16, because if formula_36, where formula_37 then 
formula_38
and formula_39, formula_40, and formula_41. Only when formula_42 do Kaprekar numbers and cycles exist. 
If formula_43 is any divisor of formula_1, then formula_3 is also a formula_1-Kaprekar number for base formula_47. 
In base formula_48, all even perfect numbers are Kaprekar numbers. More generally, any numbers of the form formula_49 or formula_50 for natural number formula_3 are Kaprekar numbers in base 2.
Set-theoric definition and unitary divisors.
We can define the set formula_52 for a given integer formula_53 as the set of integers formula_54 for which there exist natural numbers formula_55 and formula_56 satisfying the Diophantine equation
An formula_3-Kaprekar number for base formula_16 is then one which lies in the set formula_62.
It was shown in 2000 that there is a bijection between the unitary divisors of formula_63 and the set formula_52 defined above. Let formula_65 denote the multiplicative inverse of formula_66 modulo formula_67, namely the least positive integer formula_68 such that formula_69, and for each unitary divisor formula_43 of formula_63 let formula_72 and formula_73. Then the function formula_74 is a bijection from the set of unitary divisors of formula_63 onto the set formula_52. In particular, a number formula_54 is in the set formula_52 if and only if formula_79 for some unitary divisor formula_43 of formula_63.
The numbers in formula_52 occur in complementary pairs, formula_54 and formula_84. If formula_43 is a unitary divisor of formula_63 then so is formula_72, and if formula_79 then formula_89.
Kaprekar numbers for formula_12.
"b" = 4"k" + 3 and "p" = 2"n" + 1.
Let formula_24 and formula_3 be natural numbers, the number base formula_93, and formula_94. Then: 
"b" = "m"2"k" + "m" + 1 and "p" = "mn" + 1.
Let formula_68, formula_24, and formula_3 be natural numbers, the number base formula_101, and the power formula_102. Then: 
"b" = "m"2"k" + "m" + 1 and "p" = "mn" + "m" - 1.
Let formula_68, formula_24, and formula_3 be natural numbers, the number base formula_101, and the power formula_109. Then: 
"b" = "m"2"k" + "m"2 - "m" + 1 and "p" = "mn" + 1.
Let formula_68, formula_24, and formula_3 be natural numbers, the number base formula_115, and the power formula_109. Then: 
"b" = "m"2"k" + "m"2 - "m" + 1 and "p" = "mn" + "m" - 1.
Let formula_68, formula_24, and formula_3 be natural numbers, the number base formula_115, and the power formula_109. Then: 
Kaprekar numbers and cycles of formula_12 for specific formula_1, formula_16.
All numbers are in base formula_16.
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