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

Overview

More formally, given a number n = n0, define a sequence n1n2, ..., 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, 6 is unhappy, as the associated sequence is

62 = 30

32 + 02 = 9

92 = 69

62 + 92 = 99

92 + 92 = 116

12 + 12 + 62 = 32

32 + 22 = 11

12 + 12 = 2

22 = 4

42 = 14

12 + 42 = 15

12 + 52 = 22

22 + 22 = 8

82 = 54

52 + 42 = 35

32 + 52 = 2X

22 + X2 = 88

82 + 82 = X8

X2 + 82 = 118

12 + 12 + 82 = 56

52 + 62 = 51

52 + 12 = 22

22 + 22 = 8

82 = 54

52 + 42 = 35

...

The happy numbers up to 10000 are 1, 10, 100, 222, 488, 848, 884, 1000, 1113, 1131, 1311, 2022, 2202, 2220, 226X, 22X6, 262X, 26X2, 2X26, 2X62, 3111, 4088, 4808, 4880, 622X, 62X2, 6666, 66EE, 6X22, 6E6E, 6EE6, 8048, 8084, 8408, 8480, 8804, 8840, X226, X262, X622, E66E, E6E6, EE66, 10000.

The happiness of a number is unaffected by rearranging the digits and by inserting or removing any number of zeros anywhere in the number.

By inspection of the first million or so happy numbers, it appears that they have a natural density of around 0.004. Perhaps surprisingly, then, the happy numbers do not have an asymptotic density.

Sequence behavior

Numbers that are happy follow a sequence that ends in 1. All unhappy numbers follow sequences that eventually reach either the fixed points 25, X5 or one of these cycles:

{5, 21}

{8, 54, 35, 2X, 88, X8, 118, 56, 51, 22}

{18, 55, 42}

{68, 84}

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

For m = 4 and above,

n >= 10m-1 > X1m

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 EEE, and the result is 3 × X1 = 263.

• In the range 100 to 263, the number 1EE produces the largest next value, of 183.
• In the range 100 to 183, the number 17E produces the largest next value, of 123.
• In the range 100 to 123, the number 11E produces the largest next value, of X3.

Considering more precisely the intervals [264, EEE], [184, 263], [124, 183] and [100, 123], we see that every number above EE 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, EE] 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.

Happy prime

A happy prime is a number that is both happy and prime. However, there are no happy primes below 10000, the happy primes below 100000 are 11031, 1233E, 13011, 1332E, 16377, 17367, 17637, 22E8E, 2331E, 233E1, 23955, 25935, 25X8E, 28X5E, 28XE5, 2X8E5, 2E82E, 2E8X5, 31011, 31101, 3123E, 3132E, 31677, 33E21, 35295, 35567, 35765, 35925, 36557, 37167, 37671, 39525, 4878E, 4X7X7, 53567, 55367, 55637, 56357, 57635, 58XX5, 5X82E, 5XX85, 606EE, 63575, 63771, 66E0E, 67317, 67371, 67535, 6E60E, 71367, 71637, 73167, 76137, 7XX47, 82XE5, 82EX5, 8487E, 848E7, 84E87, 8874E, 8X1X7, 8X25E, 8X2E5, 8X5X5, 8XX17, 8XX71, 8E2X5, 8E847, 92355, 93255, 93525, 95235, X1X87, X258E, X285E, X2E85, X85X5, X8X17, XX477, XX585, E228E, E606E, E822E, EX825.

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, the happy numbers up to 1000 are 1, 3, X, 10, 14, 22, 23, 27, 28, 29, 2E, 30, 32, 33, 34, 37, 3E, 41, 43, 46, 4X, 4E, 55, 56, 57, 58, 64, 65, 66, 68, 72, 73, 75, 77, 7E, 82, 85, 86, 8E, 92, 99, 9E, X0, X4, XX, E2, E3, E4, E7, E8, E9, 100, 104, 111, 112, 114, 117, 11X, 121, 123, 125, 127, 128, 12E, 132, 136, 13X, 140, 141, 145, 148, 149, 14E, 152, 154, 155, 158, 163, 16X, 171, 172, 178, 179, 17E, 182, 184, 185, 187, 189, 18X, 18E, 194, 197, 198, 1X1, 1X3, 1X6, 1X8, 1E2, 1E4, 1E7, 1E8, 202, 203, 207, 208, 209, 20E, 211, 213, 215, 217, 218, 21E, 220, 223, 224, 225, 229, 230, 231, 232, 235, 236, 237, 238, 23X, 23E, 242, 244, 245, 249, 24E, 251, 252, 253, 254, 256, 257, 259, 263, 265, 267, 268, 26X, 270, 271, 273, 275, 276, 277, 278, 27E, 280, 281, 283, 286, 287, 288, 28X, 28E, 290, 292, 294, 295, 2X3, 2X6, 2X8, 2XX, 2XE, 2E0, 2E1, 2E3, 2E4, 2E7, 2E8, 2EX, 300, 302, 303, 304, 307, 30E, 312, 316, 31X, 320, 321, 322, 325, 326, 327, 328, 32X, 32E, 330, 335, 336, 338, 33E, 340, 346, 347, 34X, 34E, 352, 353, 356, 357, 361, 362, 363, 364, 365, 367, 36E, 370, 372, 374, 375, 376, 377, 379, 382, 383, 388, 389, 397, 398, 3X1, 3X2, 3X4, 3XX, 3XE, 3E0, 3E2, 3E3, 3E4, 3E6, 3EX, 401, 403, 406, 40X, 40E, 410, 411, 415, 418, 419, 41E, 422, 424, 425, 429, 42E, 430, 436, 437, 43X, 43E, 442, 444, 451, 452, 45E, 460, 463, 467, 468, 46X, 46E, 473, 476, 477, 478, 481, 486, 487, 488, 489, 48X, 491, 492, 498, 499, 49X, 4X0, 4X3, 4X6, 4X8, 4X9, 4XE, 4E0, 4E1, 4E2, 4E3, 4E5, 4E6, 4EX, 505, 506, 507, 508, 512, 514, 515, 518, 521, 522, 523, 524, 526, 527, 529, 532, 533, 536, 537, 541, 542, 54E, 550, 551, 555, 556, 557, 558, 55X, 560, 562, 563, 565, 569, 570, 572, 573, 575, 579, 580, 581, 585, 588, 589, 58X, 592, 596, 597, 598, 59X, 5X5, 5X8, 5X9, 5XE, 5E4, 5EX, 5EE, 604, 605, 606, 608, 613, 61X, 623, 625, 627, 628, 62X, 631, 632, 633, 634, 635, 637, 63E, 640, 643, 647, 648, 64X, 64E, 650, 652, 653, 655, 659, 660, 666, 66E, 672, 673, 674, 677, 678, 67E, 680, 682, 684, 687, 688, 68X, 68E, 695, 699, 69X, 69E, 6X1, 6X2, 6X4, 6X8, 6X9, 6XX, 6E3, 6E4, 6E6, 6E7, 6E8, 6E9, 702, 703, 705, 707, 70E, 711, 712, 718, 719, 71E, 720, 721, 723, 725, 726, 727, 728, 72E, 730, 732, 734, 735, 736, 737, 739, 743, 746, 747, 748, 750, 752, 753, 755, 759, 762, 763, 764, 767, 768, 76E, 770, 772, 773, 774, 776, 777, 77X, 77E, 781, 782, 784, 786, 791, 793, 795, 79E, 7X7, 7XX, 7E0, 7E1, 7E2, 7E6, 7E7, 7E9, 7EE, 802, 805, 806, 80E, 812, 814, 815, 817, 819, 81X, 81E, 820, 821, 823, 826, 827, 828, 82X, 82E, 832, 833, 838, 839, 841, 846, 847, 848, 849, 84X, 850, 851, 855, 858, 859, 85X, 860, 862, 864, 867, 868, 86X, 86E, 871, 872, 874, 876, 882, 883, 884, 885, 886, 889, 88X, 891, 893, 894, 895, 898, 899, 89E, 8X1, 8X2, 8X4, 8X5, 8X6, 8X8, 8XX, 8E0, 8E1, 8E2, 8E6, 8E9, 8EE, 902, 909, 90E, 914, 917, 918, 920, 922, 924, 925, 937, 938, 941, 942, 948, 949, 94X, 952, 956, 957, 958, 95X, 965, 969, 96X, 96E, 971, 973, 975, 97E, 981, 983, 984, 985, 988, 989, 98E, 990, 994, 996, 998, 9X4, 9X5, 9X6, 9E0, 9E6, 9E7, 9E8, X00, X04, X0X, X11, X13, X16, X18, X23, X26, X28, X2X, X2E, X31, X32, X34, X3X, X3E, X40, X43, X46, X48, X49, X4E, X55, X58, X59, X5E, X61, X62, X64, X68, X69, X6X, X77, X7X, X81, X82, X84, X85, X86, X88, X8X, X94, X95, X96, XX0, XX2, XX3, XX6, XX7, XX8, XXX, XE2, XE3, XE4, XE5, E02, E03, E04, E07, E08, E09, E12, E14, E17, E18, E20, E21, E23, E24, E27, E28, E2X, E30, E32, E33, E34, E36, E3X, E40, E41, E42, E43, E45, E46, E4X, E54, E5X, E5E, E63, E64, E66, E67, E68, E69, E70, E71, E72, E76, E77, E79, E7E, E80, E81, E82, E86, E89, E8E, E90, E96, E97, E98, EX2, EX3, EX4, EX5, EE5, EE7, EE8, EEE, 1000 (e.g. the associated sequence of 3 is 3, 23, 2E, 937, 777, 719, 755, 415, 13X, 718, 5E4, X68, 1000, 1, 1, 1, ...), and every natural numbers get to either one of these fixed points 1, 577, 668, X83, 11XX, or one of these two cycles {8, 368, 52E, X20, 700, 247, 2X7, 947, 7X8, 10X7, 940, 561, 246, 200}, {6E5, E74, 100X}.

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 there are four cycles:

{1}, {X6X, 103X8, 8256, 35X9, 9EXE, 22643, E69, 1102X, 596X, X842, 8394, 6442, 1080, 2455}, {206X, 6668, 4754}, {3X2E, 12396, 472E, X02X, E700, 9X42, 98X9, 13902}

Unlike squares and cubes, for fourth powers, there are no fixed points other than 1, but for fifth and sixth powers, there are, since there are 5-digit and 6-digit narcissistic numbers. (the largest narcissistic number is 43-digit 15079346X6E3E14EE56E395898E96629X8E01515344E4E0714E)