# Happy number

A happy number 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-ten, 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).

## 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 n = n0, define a sequence n1, n2, ..., where ni+1 is the sum of the squares of the digits of ni. Then n is happy if and only if there exists i such that ni = 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.

For example, 19 is happy, as the associated sequence is

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

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 happiness of a number is unaffected by rearranging the digits and by inserting or removing any number of zeros anywhere in the number.

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

## Sequence behavior

Numbers that are happy follow a sequence that ends in 1. All unhappy numbers follow sequences that eventually reach the eight-number cycle

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

To see this fact, first note that if n has m digits, then the sum of the squares of its digits is at most 92m, or 81m.

For m = 4 and above,

$n\geq 10^{m-1}>81m,$

so any number over 1000 gets smaller under this process and in particular becomes a number with strictly fewer digits. Once we are under 1000, the number for which the sum of squares of digits is largest is 999, and the result is 3 × 81 = 243.

• In the range 100 to 243, the number 199 produces the largest next value, of 163.
• In the range 100 to 163, the number 159 produces the largest next value, of 107.
• In the range 100 to 107, the number 107 produces the largest next value, of 50.

Considering more precisely the intervals [244, 999], [164, 243], [108, 163] and [100, 107], we see that every number above 99 gets strictly smaller under this process. Thus, no matter what number we start with, we eventually drop below 100. An exhaustive search then shows that every number in the interval [1, 99] either is happy or goes to the above unhappy cycle.

The above work produces the interesting result that no positive integer other than 1 is the sum of the squares of its own digits, since any such number would be a fixed point of the described process.

There are infinitely many happy numbers and infinitely many unhappy numbers. Consider the following proof:

• 1 is a happy number, and for every n, 10n is happy, since its sum is 1.
• For every n, 2 × 10n is unhappy, since its sum is 4, and 4 is an unhappy number.

Indeed, the happiness of a number is preserved by removing or inserting zeroes at will, since they do not contribute to the cross sum. See the above proof, especially by appending 0s on the end of the number (multiplying by 10n).

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

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.

The number of happy numbers up to 10n for 1 ≤n ≤ 20 is

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.

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

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

## Happy numbers in other bases

The definition of happy numbers depends on the decimal (base 10) representation of the numbers. The definition can be extended to other bases.

To represent numbers in other bases, we may use a subscript to the right to indicate the base. For instance, 1002 represents the number four in base 2, and

$123_{5}=1\cdot 5^{2}+2\cdot 5+3=38.$

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

$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, unhappy numbers in base b lead to cycles of numbers less than 1000b. If n < 1000b, then the sum of the squares of the base-b digits of n is less than or equal to

$3(b-1)^{2}$ ,

which can be shown to be less than b3, for b ≥ 5. This shows that once the sequence reaches a number less than 1000b, it stays below 1000b, and hence must cycle or reach 1.

In base 2, all numbers are happy. All binary numbers larger than 10002 (810) decay into a value equal to or less than 10002, and all such values are happy:

$111_{2}\rightarrow 11_{2}\rightarrow 10_{2}\rightarrow 1$
$110_{2}\rightarrow 10_{2}\rightarrow 1$
$101_{2}\rightarrow 10_{2}\rightarrow 1$
$100_{2}\rightarrow 1.$

Since all sequences lead to 1, we conclude that all numbers are happy in base 2. This makes base 2 a happy base.

The only known happy bases are 2 and 4. There are no others less than 5×108.

Base 3 is also 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. In unbalanced ternary, the digits square to 1 and 410 (113,) and in this case there are five loops: 0, 1, 2 → 113 → 2, 123 (510), and 223 (810). While all even numbers are sad because they end in the 0, 2 or 223 cycle, some odd numbers are also sad because they end in 123 or 1, and are thus occasionally sad.[citation needed]

In base 12, there is no other happy number between 10 (decimal 12) and 100 (decimal 144), there are three fixed points: 1, 2512, ↊512, and four cycles:

5 → 21 → 5
8 → 54 → 35 → 2↊ → 88 → ↊8 → 118 → 56 → 51 → 22 → 8
18 → 55 → 42 → 18
68 → 84 → 68

The numbers 2512 (2910) and ↊512 (12510) are Armstrong numbers in base 12 ; there are no two-digit Armstrong numbers in base 10.

In base 16, there is only one fixed point: 1, and one cycle:

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

The status in base 16 is similar to base 10.

## 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. For example, 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).

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

## 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:

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

def is_happy(n):