# Happy number

A happy number in base-${\displaystyle b}$ is defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits in base-${\displaystyle b}$, and repeat the process until the number either equals 1 (where it will stay), or it loops endlessly in a cycle that does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers (or sad numbers).[1]

## Origin

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).

## Overview

More formally, given a number ${\displaystyle n=n_{0}}$ of base ${\displaystyle b}$, define a sequence ${\displaystyle n_{1},n_{2},...}$ where ${\displaystyle n_{i+1}}$ is the sum of the squares of the digits of ${\displaystyle n_{i}}$.

Let ${\displaystyle n_{i}}$ be the ${\displaystyle i}$-th number of the sequence beginning with ${\displaystyle n_{0}}$. The number of digits in ${\displaystyle n_{i}}$ can be found by

${\displaystyle k_{i}=1+\lfloor \log _{b}{n_{i}}\rfloor }$,

where ${\displaystyle b}$ is the number base. The digits of ${\displaystyle n_{i}}$ can be found as follows:

${\displaystyle n_{i,0}=n_{i}{\bmod {b}}}$

and

${\displaystyle n_{i,j}={\frac {n_{i}{\bmod {b^{j+1}}}-n_{i}{\bmod {b^{j}}}}{b^{j}}}}$

where ${\displaystyle 0. The next number in the sequence is defined thus as

${\displaystyle n_{i+1}=\sum _{j=0}^{k_{i}-1}n_{i,j}^{2}}$.

A cycle is a sequence that ends in a set of numbers where two numbers in the sequence ${\displaystyle n_{i}}$ and ${\displaystyle n_{i+j}}$ are equal, where ${\displaystyle j}$ is the length of the cycle. A fixed point is a cycle where ${\displaystyle j=1}$. 0 and 1 are trivially fixed points in all bases. ${\displaystyle n}$ is happy if and only the sequence starting with ${\displaystyle n}$ ends in the fixed point of ${\displaystyle 1}$, and unhappy if it ends in any other fixed point or cycle.

For example, in base 10, 19 is happy, as the associated sequence is

12 + 92 = 82,
82 + 22 = 68,
62 + 82 = 100,
12 + 02 + 02 = 1.

If a number is happy, then all members of its sequence are happy; if a number is unhappy, all members of the sequence are unhappy. The happiness of a number is unaffected by rearranging the digits and by inserting or removing any number of zeros anywhere in the number.

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

### Upper limits to numbers in cycles

In base 2, the process of defining numbers reduces down to digit sum iteration, as ${\displaystyle 0^{3}=0}$ and ${\displaystyle 1^{3}=1}$, and iterated digit sums always reduces down to 1. Thus, we only need to consider bases ${\displaystyle b>2}$.

Numbers in base ${\displaystyle b}$ lead to cycles or fixed points of numbers less than ${\displaystyle b^{3}}$. If ${\displaystyle k_{i}\geq 4}$

${\displaystyle n_{i}\geq b^{k_{i}-1}>b^{2}k_{i},}$

so any number ${\displaystyle n_{i}\geq b^{3}}$ gets smaller as the sequence continues until ${\displaystyle n_{i}. Let ${\displaystyle n_{j}}$ be the number for which the sum of squares of digits is largest among the numbers less than ${\displaystyle b^{3}}$.

${\displaystyle n_{j}=b^{3}-1=(b-1)b^{2}+(b-1)b+(b-1)}$,
${\displaystyle n_{j+1}=3(b-1)^{2}<3b^{2}}$.

Let ${\displaystyle n_{k}}$ be the number for which the sum of squares of digits is largest among the numbers less than ${\displaystyle 3b^{2}}$.

${\displaystyle n_{k}=3b^{2}-1=2b^{2}+(b-1)b+(b-1)}$
${\displaystyle n_{k+1}=2^{2}+2(b-1)^{2}=2b^{2}-4b+6=b^{2}+(b-4)b+6<2b^{2}-1}$

Let ${\displaystyle n_{l}}$ be the number for which the sum of squares of digits is largest among the numbers less than ${\displaystyle n_{k+1}}$.

${\displaystyle n_{l}=(b-1)b+(b-1)=b^{2}-1}$
${\displaystyle n_{l+1}=2(b-1)^{2}}$

${\displaystyle n_{l}. Thus, numbers in base ${\displaystyle b}$ lead to cycles or fixed points of numbers ${\displaystyle n<1+n_{l+1}=1+2(b-1)^{2}}$. This means that only numbers ${\displaystyle n<1+2(b-1)^{2}}$ are allowed to be in fixed points or in cycles.

### Happy bases

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

A happy base is a number base ${\displaystyle b}$ that has no cycles and whose only fixed point is 1. The only known happy bases are 2 and 4. There are no others less than 5×108.[2]

## General cycles and fixed points

There are general formulas for finding cycles and nontrivial fixed points, and thus unhappy numbers, for a particular base ${\displaystyle b}$.

### Fixed points (Cycles of length 1)

#### Base b = km2 + m + k

Let ${\displaystyle m,k}$ be integers and the number base ${\displaystyle b=km^{2}+m+k>1}$.

Let the digits of ${\displaystyle n=n_{0}=n_{0,1}b+n_{0,0}}$ be ${\displaystyle n_{0,1}=k}$ and ${\displaystyle n_{0,0}=mk+1}$. Then

${\displaystyle n_{1}=(n_{0,1})^{2}+(n_{0,0})^{2}}$
${\displaystyle =k^{2}+(mk+1)^{2}}$
${\displaystyle =k^{2}+m^{2}k^{2}+2mk+1}$
${\displaystyle =(m^{2}+1)k^{2}+mk+mk+1}$
${\displaystyle =k(km^{2}+m+k)+(mk+1)}$
${\displaystyle =n_{0,1}b+n_{0,0}}$
${\displaystyle =n_{0}}$

Thus, ${\displaystyle n}$ is a fixed point for base ${\displaystyle b}$ for all ${\displaystyle m>0,k>0}$.

Let the digits of ${\displaystyle p=p_{0}=p_{0,1}b+p_{0,0}}$ be ${\displaystyle p_{0,1}=m^{2}k+m}$ and ${\displaystyle p_{0,0}=mk+1}$. Then

${\displaystyle p_{1}=(p_{0,1})^{2}+(p_{0,0})^{2}}$
${\displaystyle =(m^{2}k+m)^{2}+(mk+1)^{2}}$
${\displaystyle =(m^{2}+1)(mk+1)^{2}}$
${\displaystyle =(m^{2}+1)(mk)(mk+1)+(m^{2}+1)(mk+1)}$
${\displaystyle =(m^{3}k+mk+m^{2})(mk+1)+(mk+1)}$
${\displaystyle =m(km^{2}+m+k)(mk+1)+(mk+1)}$
${\displaystyle =p_{0,1}b+p_{0,0}}$
${\displaystyle =p_{0}}$

Thus, ${\displaystyle p}$ is a fixed point for base ${\displaystyle b}$ for all ${\displaystyle m>0,k>0}$.

The table below gives the number base (in decimal) and the fixed points (in the number base) for every ${\displaystyle m,k<5}$.

Bases and Fixed points
${\displaystyle b_{10},n,p}$ 1 2 3 4
1 3, 12, 22 5, 23, 33 7, 34, 44 9, 45, 55
2 7, 13, 63 12, 25, A5 17, 37, E7 22, 49, I9
3 13, 14, C4 23, 27, L7 33, 3A, UA 43, 4D, [39]D
4 21, 15, K5 38, 29, [36]9 55, 3D, [52]D 72, 4H, [68]H

#### Base b = Tk + 2 + (k2 + 4)m

Let ${\displaystyle m,k}$ be integers. Then the triangular number ${\displaystyle T_{k}={\frac {k(k+1)}{2}}}$ and the number base ${\displaystyle b=T_{k}+2+(k^{2}+4)m}$.

Let the digits of ${\displaystyle n=n_{0}=n_{0,1}b+n_{0,0}}$ be ${\displaystyle n_{0,1}=2(2m+1)}$ and ${\displaystyle n_{0,0}=(2m+1)k+1}$. Then

${\displaystyle n_{1}=(n_{0,1})^{2}+(n_{0,0})^{2}}$
${\displaystyle =(2(2m+1))^{2}+(((2m+1))k+1)^{2}}$
${\displaystyle =4(2m+1)^{2}+(2m+1)^{2}k^{2}+2(2m+1)k+1}$
${\displaystyle =(4+k^{2})(2m+1)^{2}+(2m+1)k+((2m+1)k+1)}$
${\displaystyle =(4(2m+1)+k^{2}(2m+1)+k)(2m+1)+((2m+1)k+1)}$
${\displaystyle =2(2m+1)(2(2m+1)+k^{2}m+{\frac {k^{2}+k}{2}})+((2m+1)k+1)}$
${\displaystyle =2(2m+1)(4m+2+k^{2}m+T_{k})+((2m+1)k+1)}$
${\displaystyle =2(2m+1)((4+k^{2})m+T_{k}+2)+((2m+1)k+1)}$
${\displaystyle =n_{0,1}b+n_{0,0}}$
${\displaystyle =n_{0}}$

Thus, ${\displaystyle n}$ is a fixed point for base ${\displaystyle b}$ for all ${\displaystyle m>0,k>0}$.

Let the digits of ${\displaystyle p=p_{0}=p_{0,1}b+p_{0,0}}$ be ${\displaystyle p_{0,1}=T_{k}+k^{2}m}$ and ${\displaystyle p_{0,0}=(2m+1)k+1}$. Then

${\displaystyle p_{1}=(p_{0,1})^{2}+(p_{0,0})^{2}}$
${\displaystyle =((T_{k}+k^{2}m))^{2}+((2m+1)k+1)^{2}}$
${\displaystyle =((T_{k}+k^{2}m))^{2}+(2m+1)^{2}k^{2}+2(2m+1)k+1}$
${\displaystyle =((T_{k}+k^{2}m))^{2}+(4m^{2}+4m+1)k^{2}+(2m+1)k+((2m+1)k+1)}$
${\displaystyle =((T_{k}+k^{2}m))^{2}+(4m^{2}+2m)k^{2}+(2m+1)k^{2}+(2m+1)k+((2m+1)k+1)}$
${\displaystyle =((T_{k}+k^{2}m))^{2}+2(2m+1)mk^{2}+2(2m+1)T_{k}+((2m+1)k+1)}$
${\displaystyle =((T_{k}+k^{2}m))^{2}+(4m+2)(T_{k}+k^{2}m)+((2m+1)k+1)}$
${\displaystyle =(T_{k}+k^{2}m+4m+2)(T_{k}+k^{2}m)+((2m+1)k+1)}$
${\displaystyle =(T_{k}+2+(k^{2}+4)m)(T_{k}+k^{2}m)+((2m+1)k+1)}$
${\displaystyle =p_{0,1}b+p_{0,0}}$
${\displaystyle =p_{0}}$

Thus, ${\displaystyle p}$ is a fixed point for base ${\displaystyle b}$ for all ${\displaystyle m>0,k>0}$.

The table below gives the number base (in decimal) and the fixed points (in the number base) for every ${\displaystyle m<5,k<5}$.

Bases and Fixed points
${\displaystyle b_{10},n,p}$ 1 2 3 4
1 3, 22, 12 5, 23, 33 8, 24, 64 12, 25, A5
2 8, 64, 24 13, 67, 77 21, 6A, FA 32, 6D, QD
3 13, A6, 36 21, AB, BB 34, AG, OG 52, AL, [42]L
4 18, E8, 48 29, EF, FF 57, EM, XM 72, ET, [58]T

### Cycles of length 2

#### Base b = k3

Let ${\displaystyle k}$ be a positive integer and the number base ${\displaystyle b=k^{3}}$.

Let ${\displaystyle n_{0}=k^{2}}$. Then

${\displaystyle n_{1}=(k^{2})^{2}}$
${\displaystyle =kk^{3}}$
${\displaystyle =kb}$
${\displaystyle n_{2}=k^{2}+0^{2}}$
${\displaystyle =k^{2}}$
${\displaystyle =n_{0}}$

Thus, ${\displaystyle kb}$ and ${\displaystyle k^{2}}$ form a cycle of length 2 for base ${\displaystyle b}$ for all ${\displaystyle k>1}$.

${\displaystyle k}$ ${\displaystyle b}$ Cycles
2 8 4 → 20 → 4
3 27 9 → 30 → 9
4 64 G → 40 → G

#### Base b = k3 + 2k

Let ${\displaystyle k}$ be a positive integer and the number base ${\displaystyle b=k^{3}+2k}$.

Let ${\displaystyle n_{0}=k^{2}+1}$. Then

${\displaystyle n_{1}=(k^{2}+1)^{2}}$
${\displaystyle =k^{4}+2k^{2}+1}$
${\displaystyle =k(k^{3}+2k)+1}$
${\displaystyle =kb+1}$
${\displaystyle n_{2}=k^{2}+1^{2}}$
${\displaystyle =k^{2}+1}$
${\displaystyle =n_{0}}$

Thus, ${\displaystyle kb}$ and ${\displaystyle k^{2}+1}$ form a cycle of length 2 for base ${\displaystyle b}$ for all ${\displaystyle k\geq 1}$.

${\displaystyle k}$ ${\displaystyle b}$ Cycles
1 3 2 → 11 → 2
2 12 5 → 21 → 5
3 33 A → 31 → A

## Happy numbers in specific bases

### Decimal

It has been proven above that in base ${\displaystyle b}$ one only needs to check numbers ${\displaystyle n<1+2(b-1)^{2}}$ for cycles and fixed points, as all numbers above ${\displaystyle 2(b-1)^{2}}$ will eventually fall below ${\displaystyle 2(b-1)^{2}}$. In base 10, that upper limit is ${\displaystyle 1+2(10-1)^{2}=163}$. 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.[3] The first set of three consecutive is 1880, 1881, and 1882.[4] It has been proved that there exist sequences of consecutive happy numbers of any natural-number length.[5] The beginning of the first run of at least n consecutive happy numbers for n = 1, 2, 3, ... is[6]

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

By inspection of the first million or so happy numbers, it appears that they have a natural density of around 0.15. Perhaps surprisingly, then, the 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.[7]

The number of 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 prime

A happy prime is a number that is both happy and prime. The 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).

All numbers, and therefore all primes, of the form 10n + 3 or 10n + 9 for n greater than 0 are happy.[clarification needed] To see this, note that

• All such numbers will have at least two digits.
• The first digit will always be 1 due to the 10n.
• The last digit will always be either 3 or 9.
• Any other digits will always be 0 (and therefore will not contribute to the sum of squares of the digits).
• The sequence for numbers ending in 3 is: 12 + 32 = 10 → 12 = 1.
• The sequence for numbers ending in 9 is: 12 + 92 = 82 → 82 + 22 = 64 + 4 = 68 → 62 + 82 = 36 + 64 = 100 → 1.

(This does not mean that these are the only happy primes, as evidenced by the sequence above.)

The palindromic prime 10150006 + 7426247×1075000 + 1 is also a 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 happy number. Paul Jobling discovered the prime in 2005.[9]

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

Unlike happy numbers, rearranging the digits of a happy prime will not necessarily create another happy prime. For instance, while 19 is a happy prime, 91 = 13 × 7 is not prime (but is still happy).

### Balanced ternary

Balanced ternary is a special case in that the happiness (or sadness) of a number is an indication also of being odd (or even). Specifically, because 3 − 1 = 2, the sum of every digit of a base-3 number will indicate divisibility by 2 if and only if the sum of digits ends in 0 or 2. This is the general application of the test for divisibility by 9 in base 10. In balanced ternary, the digits are 1, −1 and 0. The square of both 1 and −1 are 1, and 1 + 1 is 2, which is the only balanced ternary cycle. For every pair of digits 1 or −1, their sum is 0 and the sum of their squares is 2 and if there are an even number of {1, −1} sets, the number divisible by 2 and sad and if odd, it is happy. In this case, the result always end in a one-digit cycle of 0, 1 or 2, repeated infinitely.[citation needed]

### Other bases

Base Cycles
5 4 → 31 → 20 → 4
6 5 → 41 → 25 → 45 → 105 → 42 → 32 → 21 → 5
7 2 → 4 → 22 → 11 → 2
8 5 → 31 → 12 → 5

15 → 32 → 15

9 58 → 108 → 72 → 58
10 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4
12 8 → 54 → 35 → 2A → 88 → A8 → 118 → 56 → 51 → 22 → 8

18 → 55 → 42 → 18

68 → 84 → 68

14 5 → 1B → 8A → BA → 11B → 8B → D3 → CA → 136 → 34 → 5

29 → 85 → 61 → 29

16 D → A9 → B5 → 92 → 55 → 32 → D

## Cubing the digits rather than squaring

A variation to the happy numbers problem is to find the sum of the cubes of the digits rather than the sum of the squares of the digits.

Given a number ${\displaystyle n=n_{0}}$ of base ${\displaystyle b}$, define a sequence ${\displaystyle n_{1},n_{2},...}$ where ${\displaystyle n_{i+1}}$ is the sum of the cubes of the digits of ${\displaystyle n_{i}}$.

Let ${\displaystyle n_{i}}$ be the ${\displaystyle i}$-th number of the sequence beginning with ${\displaystyle n_{0}}$. The number of digits in ${\displaystyle n_{i}}$ can be found by

${\displaystyle k_{i}=1+\lfloor \log _{b}{n_{i}}\rfloor }$,

where ${\displaystyle b}$ is the number base. The digits of ${\displaystyle n_{i}}$ can be found as follows:

${\displaystyle n_{i,0}=n_{i}{\bmod {b}}}$

and

${\displaystyle n_{i,j}={\frac {n_{i}{\bmod {b^{j+1}}}-n_{i}{\bmod {b^{j}}}}{b^{j}}}}$

where ${\displaystyle 0. The next number in the sequence is defined thus as

${\displaystyle n_{i+1}=\sum _{j=0}^{k_{i}-1}n_{i,j}^{3}}$.

A cycle is a sequence where two numbers in the sequence ${\displaystyle n_{i}}$ and ${\displaystyle n_{i+j}}$ are equal, where ${\displaystyle j}$ is the length of the cycle. A fixed point is a cycle where ${\displaystyle j=1}$. 0 and 1 are trivially fixed points in all bases. ${\displaystyle n}$ is happy if and only the sequence starting with ${\displaystyle n}$ ends in the fixed point of ${\displaystyle 1}$.

### Upper limits to numbers in cycles

Numbers in base ${\displaystyle b}$ lead to cycles or fixed points of numbers less than ${\displaystyle b^{4}}$. If ${\displaystyle k_{i}\geq 5}$

${\displaystyle n_{i}\geq b^{k_{i}-1}>b^{3}k_{i},}$

so any number ${\displaystyle n_{i}\geq b^{4}}$ gets smaller as the sequence continues until ${\displaystyle n_{i}.

### Base 10

Working in base 10, 1579 is happy, since:

13 + 53 + 73 + 93 = 1 + 125 + 343 + 729 = 1198
13 + 13 + 93 + 83 = 1 + 1 + 729 + 512 = 1243
13 + 23 + 43 + 33 = 1 + 8 + 64 + 27 = 100
13 + 03 + 03 = 1

In the same way that when summing the squares of the digits (and working in base 10) each number above 243 (= 3 × 92) produces a number that is strictly smaller, when summing the cubes of the digits each number above 2916 (= 4 × 93) produces a number that is strictly smaller.

By conducting an exhaustive search of [1, 2916] one finds that for summing the cubes of digits base 10 there are happy numbers and eight different types of unhappy number:

• those that eventually reach 153, 370, 371, or 407, which perpetually produce themselves; and
• those that eventually reach one of the loops:
133 → 55 → 250 → 133
217 → 352 → 160 → 217
1459 → 919 → 1459
136 → 244 →136

All multiples of three terminate at 153. This fact can be proved by the exhaustive search up and noting that a number is a multiple of three if and only if the sum of digits is a multiple of three if and only if the sum of its cubed digits are a multiple of three. By similar reasoning, all happy numbers when summing up odd powers (e.g. cubes, fifth powers, seventh powers, etc.) of their digits must have a remainder of 1 when dividing by 3.

All numbers that are congruent to 2 modulo 3 terminate at either 371 or 407.

The only positive whole numbers that are the sum of the cubes of their digits are 1, 153, 370, 371 and 407 (sequence A046197 in the OEIS).

### Other bases

In base 2, the process of defining numbers reduces down to digit sum iteration, as ${\displaystyle 0^{3}=0}$ and ${\displaystyle 1^{3}=1}$, and iterated digit sums always reduces down to 1, making base 2 a happy base.

## Higher powers

For higher powers, the density of happy numbers declines.

Taking the sum of the fourth powers of the digits, one can find that most numbers between 1 and 100 end in the loop:

13139 → 6725 → 4338 → 4514 → 1138 → 4179 → 9219 → 13139,

or alternate between 2178 and 6514, or terminate at 1634, 8208 or 9474 which perpetually produce themselves.

### Other bases

It is easy to see that there are infinitely many happy numbers in every base. For instance, the numbers

${\displaystyle 1_{b},10_{b},100_{b},1000_{b},...\quad (=b^{n}{\text{ for all positive integers }}n)}$

are all happy, for any base b.

By a similar argument to the one above for decimal happy numbers, for a given power ${\displaystyle p}$ unhappy numbers in base ${\displaystyle b}$ lead to cycles of numbers less than ${\displaystyle (10^{p+1})_{b}}$. If ${\displaystyle n<(10^{p+1})_{b}}$, then the sum of the squares of the base-b digits of ${\displaystyle n}$ is less than or equal to

${\displaystyle (p+1)(b-1)^{p}}$,

which is less than ${\displaystyle b^{p+1}}$ for all bases, as the polynomial of degree ${\displaystyle p}$

${\displaystyle f(b)=b^{p+1}-(p+1)(b-1)^{p}}$

is monotonically increasing and has only one root ${\displaystyle b<1}$ if ${\displaystyle p}$ is even, and is always positive if ${\displaystyle p}$ is odd. This shows that once the sequence reaches a number less than ${\displaystyle (10^{p+1})_{b}}$, it stays below ${\displaystyle (10^{p+1})_{b}}$, and hence must cycle or reach 1.

In base 2, the process of defining numbers reduces down to digit sum iteration, as ${\displaystyle 0^{p}=0}$ and ${\displaystyle 1^{p}=1}$, and iterated digit sums always reduces down to 1, making base 2 a happy base.

## 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. ^ Sloane, N. J. A. (ed.). "Sequence A161872 (Smallest unhappy number in base n)". The On-Line Encyclopedia of Integer Sequences. OEIS Foundation.
3. ^ 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.
4. ^ 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.
5. ^ Pan, Hao (2006). "Consecutive Happy Numbers". arXiv:math/0607213.
6. ^
7. ^ Gilmer, Justin (2011). "On the Density of Happy Numbers". Integers. 13 (2). arXiv:1110.3836. Bibcode:2011arXiv1110.3836G.
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