Perfect digital invariant

In number theory, a perfect digital invariant (PDI) is a number in a given number base formula_1 that is the sum of its own digits each raised to a given power formula_2.
Definition.
Let formula_3 be a natural number. We define the perfect digital invariant function (also known as a happy function, from happy numbers) for base formula_4 and power formula_5 formula_6 to be the following:
where formula_8 is the number of digits in the number in base formula_1, and 
is the value of each digit of the number. A natural number formula_3 is a perfect digital invariant if it is a fixed point for formula_12, which occurs if formula_13. formula_14 and formula_15 are trivial perfect digital invariants for all formula_1 and formula_2, all other perfect digital invariants are nontrivial perfect digital invariants.
For example, the number 4150 in base formula_18 is a perfect digital invariant with formula_19, because formula_20.
A natural number formula_3 is a sociable digital invariant if it is a periodic point for formula_12, where formula_23 for a positive integer formula_24 (here formula_25 is the formula_24th iterate of formula_27), and forms a cycle of period formula_24. A perfect digital invariant is a sociable digital invariant with formula_29, and a amicable digital invariant is a sociable digital invariant with formula_30.
All natural numbers formula_3 are preperiodic points for formula_12, regardless of the base. This is because if formula_33, formula_34, so any formula_3 will satisfy formula_36 until formula_37. There are a finite number of natural numbers less than formula_38, so the number is guaranteed to reach a periodic point or a fixed point less than formula_39, making it a preperiodic point.
Numbers in base formula_40 lead to fixed or periodic points of numbers formula_41.
The number of iterations formula_42 needed for formula_43 to reach a fixed point is the perfect digital invariant function's persistence of formula_3, and undefined if it never reaches a fixed point.
formula_45 is the digit sum. The only perfect digital invariants are the single-digit numbers in base formula_1, and there are no periodic points with prime period greater than 1.
formula_47 reduces to formula_48, as for any power formula_2, formula_50 and formula_51.
For every natural number formula_52, if formula_53, formula_54 and formula_55, then for every natural number formula_3, if formula_57, then formula_58, where formula_59 is Euler's totient function.
No upper bound can be determined for the size of perfect digital invariants in a given base and arbitrary power, and it is not currently known whether or not the number of perfect digital invariants for an arbitrary base is finite or infinite.
Perfect digital invariants of "F"2,"b".
By definition, any three-digit perfect digital invariant formula_60 for formula_61 with natural number digits formula_62, formula_63, formula_64 has to satisfy the cubic Diophantine equation formula_65. However, formula_66 has to be equal to 0 or 1 for any formula_67, because the maximum value formula_3 can take is formula_69. As a result, there are actually two related quadratic Diophantine equations to solve:
The two-digit natural number formula_74 is a perfect digital invariant in base 
This can be proven by taking the first case, where formula_71, and solving for formula_1. This means that for some values of formula_78 and formula_79, formula_3 is not a perfect digital invariant in any base, as formula_79 is not a divisor of formula_82. Moreover, formula_83, because if formula_84 or formula_85, then formula_86, which contradicts the earlier statement that formula_63.
There are no three-digit perfect digital invariants for formula_61, which can be proven by taking the second case, where formula_73, and letting formula_90 and formula_91. Then the Diophantine equation for the three-digit perfect digital invariant becomes 
However, formula_96 for all values of formula_97. Thus, there are no solutions to the Diophantine equation, and there are no three-digit perfect digital invariants for formula_61.
Perfect digital invariants of "F"3,"b".
By definition, any four-digit perfect digital invariant formula_3 for formula_100 with natural number digits formula_62, formula_63, formula_64, formula_104 has to satisfy the quartic Diophantine equation formula_105. However, formula_106 has to be equal to 0, 1, 2 for any formula_107, because the maximum value formula_3 can take is formula_109. As a result, there are actually three related cubic Diophantine equations to solve
We take the first case, where formula_111.
"b" = 3"k" + 1.
Let formula_24 be a positive integer and the number base formula_118. Then:
"b" = 3"k" + 2.
Let formula_24 be a positive integer and the number base formula_129. Then:
"b" = 6"k" + 4.
Let formula_24 be a positive integer and the number base formula_134. Then:
Perfect digital invariants and cycles of "F""p","b" for specific "p" and "b".
All numbers are represented in base formula_1. 
Extension to negative integers.
Perfect digital invariants can be extended to the negative integers by use of a signed-digit representation to represent each integer.
Balanced ternary.
In balanced ternary, the digits are 1, −1 and 0. This results in the following:
Relation to happy numbers.
A happy number formula_3 for a given base formula_1 and a given power formula_2 is a preperiodic point for the perfect digital invariant function formula_12 such that the formula_152-th iteration of formula_12 is equal to the trivial perfect digital invariant formula_15, and an unhappy number is one such that there exists no such formula_152.
Programming example.
The example below implements the perfect digital invariant function described in the definition above to search for perfect digital invariants and cycles in Python. This can be used to find happy numbers.
def pdif(x: int, p: int, b: int) -> int:
 """Perfect digital invariant function."""
 total = 0
 while x > 0:
 total = total + pow(x % b, p)
 x = x // b
 return total
def pdif_cycle(x: int, p: int, b: int) -> List[int]:
 seen = []
 while x not in seen:
 seen.append(x)
 x = pdif(x, p, b)
 cycle = []
 while x not in cycle:
 cycle.append(x)
 x = pdif(x, p, b)
 return cycle