Happy number

In number theory, a happy number is a number which eventually reaches 1 when replaced by the sum of the square of each digit. For instance, 13 is a happy number because formula_1, and formula_2. On the other hand, 4 is not a happy number because the sequence starting with formula_3 and formula_4 eventually reaches formula_5, the number that started the sequence, and so the process continues in an infinite cycle without ever reaching 1. A number which is not happy is called sad or unhappy.
More generally, a formula_6-happy number is a natural number in a given number base formula_6 that eventually reaches 1 when iterated over the perfect digital invariant function for formula_8.
The origin of happy numbers is not clear. Happy numbers were brought to the attention of Reg Allenby (a British author and senior lecturer in pure mathematics at Leeds University) by his daughter, who had learned of them at school. However, they "may have originated in Russia" .
Happy numbers and perfect digital invariants.
Formally, let formula_9 be a natural number. Given the perfect digital invariant function 
for base formula_11, a number formula_9 is formula_6-happy if there exists a formula_14 such that formula_15, where formula_16 represents the formula_14-th iteration of formula_18, and formula_6-unhappy otherwise. If a number is a nontrivial perfect digital invariant of formula_18, then it is formula_6-unhappy.
For example, 19 is 10-happy, as
For example, 347 is 6-happy, as
There are infinitely many formula_6-happy numbers, as 1 is a formula_6-happy number, and for every formula_9, formula_32 (formula_33 in base formula_6) is formula_6-happy, since its sum is 1. The "happiness" of a number is preserved by removing or inserting zeroes at will, since they do not contribute to the cross sum.
Natural density of formula_6-happy numbers.
By inspection of the first million or so 10-happy numbers, it appears that they have a natural density of around 0.15. Perhaps surprisingly, then, the 10-happy numbers do not have an asymptotic density. The upper density of the happy numbers is greater than 0.18577, and the lower density is less than 0.1138.
Happy bases.
A happy base is a number base formula_6 where every number is formula_6-happy. The only happy bases less than are base 2 and base 4.
Specific formula_6-happy numbers.
4-happy numbers.
For formula_40, the only positive perfect digital invariant for formula_18 is the trivial perfect digital invariant 1, and there are no other cycles. Because all numbers are preperiodic points for formula_18, all numbers lead to 1 and are happy. As a result, base 4 is a happy base.
6-happy numbers.
For formula_43, the only positive perfect digital invariant for formula_18 is the trivial perfect digital invariant 1, and the only cycle is the eight-number cycle
and because all numbers are preperiodic points for formula_18, all numbers either lead to 1 and are happy, or lead to the cycle and are unhappy. Because base 6 has no other perfect digital invariants except for 1, no positive integer other than 1 is the sum of the squares of its own digits.
In base 10, the 74 6-happy numbers up to 1296 = 64 are (written in base 10):
10-happy numbers.
For formula_46, the only positive perfect digital invariant for formula_18 is the trivial perfect digital invariant 1, and the only cycle is the eight-number cycle
and because all numbers are preperiodic points for formula_18, all numbers either lead to 1 and are happy, or lead to the cycle and are unhappy. Because base 10 has no other perfect digital invariants except for 1, no positive integer other than 1 is the sum of the squares of its own digits.
In base 10, the 143 10-happy numbers up to 1000 are:
The distinct combinations of digits that form 10-happy numbers below 1000 are (the rest are just rearrangements and/or insertions of zero digits):
The first pair of consecutive 10-happy numbers is 31 and 32. The first set of three consecutive is 1880, 1881, and 1882. It has been proven that there exist sequences of consecutive happy numbers of any natural number length. The beginning of the first run of at least "n" consecutive 10-happy numbers for "n" = 1, 2, 3, ... is
As Robert Styer puts it in his paper calculating this series: "Amazingly, the same value of N that begins the least sequence of six consecutive happy numbers also begins the least sequence of seven consecutive happy numbers."
The number of 10-happy numbers up to 10"n" for 1 ≤ "n" ≤ 20 is
Happy primes.
A formula_6-happy prime is a number that is both formula_6-happy and prime. Unlike happy numbers, rearranging the digits of a formula_6-happy prime will not necessarily create another happy prime. For instance, while 19 is a 10-happy prime, 91 = 13 × 7 is not prime (but is still 10-happy).
All prime numbers are 2-happy and 4-happy primes, as base 2 and base 4 are happy bases.
6-happy primes.
In base 6, the 6-happy primes below 1296 = 64 are
10-happy primes.
In base 10, the 10-happy primes below 500 are
The palindromic prime is a 10-happy prime with digits because the many 0s do not contribute to the sum of squared digits, and = 176, which is a 10-happy number. Paul Jobling discovered the prime in 2005.
, the largest known 10-happy prime is 242643801 − 1 (a Mersenne prime). Its decimal expansion has digits.
12-happy primes.
In base 12, there are no 12-happy primes less than 10000, the first 12-happy primes are (the letters X and E represent the decimal numbers 10 and 11 respectively)
Programming example.
The examples below implement the perfect digital invariant function for formula_8 and a default base formula_46 described in the definition of happy given at the top of this article, repeatedly; after each time, they check for both halt conditions: reaching 1, and repeating a number.
A simple test in Python to check if a number is happy:
def pdi_function(number, base: int = 10):
 """Perfect digital invariant function."""
 total = 0
 while number > 0:
 total += pow(number % base, 2)
 number = number // base
 return total
def is_happy(number: int) -> bool:
 """Determine if the specified number is a happy number."""
 seen_numbers = set()
 while number > 1 and number not in seen_numbers:
 seen_numbers.add(number)
 number = pdi_function(number)
 return number == 1