# Happy number

A ${\displaystyle b}$-happy number is a natural number in a given number base ${\displaystyle b}$ that eventually reaches 1 when iterated over the perfect digital invariant function for ${\displaystyle p=2}$. Those numbers that do not end in 1 are ${\displaystyle b}$-unhappy numbers (or ${\displaystyle b}$-sad numbers).[1]

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" (Guy 2004:§E34).

## Happy numbers and perfect digital invariants

More formally, let ${\displaystyle n}$ be a natural number. Given the perfect digital invariant function

${\displaystyle F_{p,b}(n)=\sum _{i=0}^{\lfloor \log _{b}{n}\rfloor }{\left({\frac {n{\bmod {b^{i+1}}}-n{\bmod {b^{i}}}}{b^{i}}}\right)}^{p}}$.

for base ${\displaystyle b>1}$, a number ${\displaystyle n}$ is ${\displaystyle b}$-happy if there exists a ${\displaystyle j}$ such that ${\displaystyle F_{2,b}^{j}(n)=1}$, where ${\displaystyle F_{2,b}^{j}}$ represents the ${\displaystyle j}$-th iteration of ${\displaystyle F_{2,b}}$, and ${\displaystyle b}$-unhappy otherwise. If a number is a nontrivial perfect digital invariant of ${\displaystyle F_{2,b}}$, then it is ${\displaystyle b}$-unhappy.

For example, 19 is 10-happy, as

${\displaystyle F_{2,10}(19)=1^{2}+9^{2}=82}$
${\displaystyle F_{2,10}^{2}(19)=F_{2,10}(82)=8^{2}+2^{2}=68}$
${\displaystyle F_{2,10}^{3}(19)=F_{2,10}(68)=6^{2}+8^{2}=100}$
${\displaystyle F_{2,10}^{4}(19)=F_{2,10}(100)=1^{2}+0^{2}+0^{2}=1}$

For example, 347 is 6-happy, as

${\displaystyle F_{2,6}(347)=F_{2,6}(1335_{6})=1^{2}+3^{2}+3^{2}+5^{2}=44}$
${\displaystyle F_{2,6}^{2}(347)=F_{2,6}(44)=H_{2,6}(112_{6})=1^{2}+1^{2}+2^{2}=6}$
${\displaystyle F_{2,6}^{3}(347)=F_{2,6}(6)=H_{2,6}(10_{6})=1^{2}+0^{2}=1}$

There are infinitely many ${\displaystyle b}$-happy numbers, as 1 is a ${\displaystyle b}$-happy number, and for every ${\displaystyle n}$, ${\displaystyle b^{n}}$ (${\displaystyle 10^{n}}$ in base ${\displaystyle b}$) is ${\displaystyle b}$-happy, since its sum is 1. Indeed, 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 ${\displaystyle b}$-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.[2]

### Happy bases

 Unsolved problem in mathematics:Are base 2 and base 4 the only bases that are happy?(more unsolved problems in mathematics)

A happy base is a number base ${\displaystyle b}$ where every number is ${\displaystyle b}$-happy. The only happy bases less than 5×108 are base 2 and base 4.[3]

## Specific ${\displaystyle b}$-happy numbers

### 4-happy numbers

It is proved here that for ${\displaystyle p=2}$ in base ${\displaystyle b>2}$ one only needs to check numbers ${\displaystyle n\leq (2-2)^{2}+2(b-1)^{2}=2(b-1)^{2}}$ for cycles and fixed points of ${\displaystyle F_{2,b}}$. For ${\displaystyle b=4}$, that upper limit is ${\displaystyle 2\cdot 3^{2}=18_{10}=102_{4}}$. The following sequences lead to the fixed point 1:

• 3 → 21 → 11 → 2 → 10 → 1
• 23 → 31 → 22 → 20 → 10 → 1
• 33 → 102 → 11 → 2 → 10 → 1

With rearrangements and/or insertions of zero digits, this shows that every number in the interval [1, 102] is happy. As a result, every number in base 4 is happy, and base 4 is a happy base.

### 10-happy numbers

It is proved here that for ${\displaystyle p=2}$ in base ${\displaystyle b>2}$ one only needs to check numbers ${\displaystyle n\leq (2-2)^{2}+2(b-1)^{2}=2(b-1)^{2}}$ for cycles and fixed points of ${\displaystyle F_{2,b}}$. For ${\displaystyle b=10}$, that upper limit is ${\displaystyle 2\cdot 9^{2}=162}$. An exhaustive search then shows that every number in the interval [1, 162] eventually reaches either the eight-number cycle

4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 → ...

and is unhappy or the trivial fixed point 1 and is happy. Because base 10 has no other fixed points except for 1, no positive integer other than 1 is the sum of the squares of its own digits.

In base 10, the 143 happy numbers up to 1000 are:

1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100, 103, 109, 129, 130, 133, 139, 167, 176, 188, 190, 192, 193, 203, 208, 219, 226, 230, 236, 239, 262, 263, 280, 291, 293, 301, 302, 310, 313, 319, 320, 326, 329, 331, 338, 356, 362, 365, 367, 368, 376, 379, 383, 386, 391, 392, 397, 404, 409, 440, 446, 464, 469, 478, 487, 490, 496, 536, 556, 563, 565, 566, 608, 617, 622, 623, 632, 635, 637, 638, 644, 649, 653, 655, 656, 665, 671, 673, 680, 683, 694, 700, 709, 716, 736, 739, 748, 761, 763, 784, 790, 793, 802, 806, 818, 820, 833, 836, 847, 860, 863, 874, 881, 888, 899, 901, 904, 907, 910, 912, 913, 921, 923, 931, 932, 937, 940, 946, 964, 970, 973, 989, 998, 1000 (sequence A007770 in the OEIS).

The distinct combinations of digits that form happy numbers below 1000 are (the rest are just rearrangements and/or insertions of zero digits):

1, 7, 13, 19, 23, 28, 44, 49, 68, 79, 129, 133, 139, 167, 188, 226, 236, 239, 338, 356, 367, 368, 379, 446, 469, 478, 556, 566, 888, 899. (sequence A124095 in the OEIS).

The first pair of consecutive happy numbers is 31 and 32.[4] The first set of three consecutive is 1880, 1881, and 1882.[5] It has been proved that there exist sequences of consecutive happy numbers of any natural-number length.[6] The beginning of the first run of at least n consecutive happy numbers for n = 1, 2, 3, ... is[7]

1, 31, 1880, 7839, 44488, 7899999999999959999999996, 7899999999999959999999996, ...

The number of 10-happy numbers up to 10n for 1 ≤n ≤ 20 is[8]

3, 20, 143, 1442, 14377, 143071, 1418854, 14255667, 145674808, 1492609148, 15091199357, 149121303586, 1443278000870, 13770853279685, 130660965862333, 1245219117260664, 12024696404768025, 118226055080025491, 1183229962059381238, 12005034444292997294.

## Happy primes

A ${\displaystyle b}$-happy prime is a number that is both ${\displaystyle b}$-happy and prime. Unlike happy numbers, rearranging the digits of a ${\displaystyle b}$-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.

### 10-happy primes

In base 10, the 10-happy primes below 500 are

7, 13, 19, 23, 31, 79, 97, 103, 109, 139, 167, 193, 239, 263, 293, 313, 331, 367, 379, 383, 397, 409, 487 (sequence A035497 in the OEIS).

The palindromic prime 10150006 + 7426247×1075000 + 1 is a 10-happy prime with 150007 digits because the many 0s do not contribute to the sum of squared digits, and 12 + 72 + 42 + 22 + 62 + 22 + 42 + 72 + 12 = 176, which is a 10-happy number. Paul Jobling discovered the prime in 2005.[9]

As of 2010, the largest known 10-happy prime is 242643801 − 1 (a Mersenne prime).[dubious ] Its decimal expansion has 12837064 digits.[10]

## Programming example

The examples below apply the 'happy' process 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. Everything else is bookkeeping (for example, the Python example precomputes the squares of all 10 digits).

A simple test in Python to check if a number is happy:[11]

def square(x):
return int(x) * int(x)

def happy(number):
return sum(map(square, list(str(number))))

def is_happy(number):
seen_numbers = set()
while number > 1 and number not in seen_numbers:
number = happy(number)
return number == 1


When the algorithm ends in a cycle of repeating numbers, this cycle always includes the number 4, so it is not even necessary to store previous numbers in the sequence:

def is_happy(n):
return n == 1 or n > 4 and is_happy(happy(n))


## References

1. ^ "Sad Number". Wolfram Research, Inc. Retrieved 16 September 2009.
2. ^ Gilmer, Justin (2011). "On the Density of Happy Numbers". Integers. 13 (2). arXiv:1110.3836. Bibcode:2011arXiv1110.3836G.
3. ^ Sloane, N. J. A. (ed.). "Sequence A161872 (Smallest unhappy number in base n)". The On-Line Encyclopedia of Integer Sequences. OEIS Foundation.
4. ^ Sloane, N. J. A. (ed.). "Sequence A035502 (Lower of pair of consecutive happy numbers)". The On-Line Encyclopedia of Integer Sequences. OEIS Foundation. Retrieved 8 April 2011.
5. ^ Sloane, N. J. A. (ed.). "Sequence A072494 (First of triples of consecutive happy numbers)". The On-Line Encyclopedia of Integer Sequences. OEIS Foundation. Retrieved 8 April 2011.
6. ^ Pan, Hao (2006). "Consecutive Happy Numbers". arXiv:math/0607213.
7. ^
8. ^ Sloane, N. J. A. (ed.). "Sequence A068571 (Number of happy numbers <= 10^n)". The On-Line Encyclopedia of Integer Sequences. OEIS Foundation.
9. ^ Chris K. Caldwell. "The Prime Database: 10150006 + 7426247 · 1075000 + 1". utm.edu.
10. ^ Chris K. Caldwell. "The Prime Database: 242643801 − 1". utm.edu.
11. ^ Happy Number Rosetta Code