Old page wikitext, before the edit (old_wikitext ) | '{{distinguish2|[[Harshad number]] (derived from Sanskrit ''harsa'': "great joy")}}
A '''happy number''' is a number defined by the following process: Starting with any [[positive number|positive]] [[integer]], replace the number by the [[sum]] of the squares of its [[numerical digit|digits]], and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which 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''').<ref>{{cite web|url=http://mathworld.wolfram.com/SadNumber.html|title=Sad Number|publisher=Wolfram Research, Inc.|accessdate=2009-09-16}}</ref>
==Overview==
More formally, given a number <math>n=n_0</math>, define a sequence <math>n_1</math>, <math>n_2</math>, ... where <math>n_{i+1}</math> is the sum of the squares of the digits of <math>n_i</math>. Then ''n'' is happy if and only if there exists ''i'' such that <math>n_i = 1</math>.
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:
:1<sup>2</sup> + 9<sup>2</sup> = 82
:8<sup>2</sup> + 2<sup>2</sup> = 68
:6<sup>2</sup> + 8<sup>2</sup> = 100
:1<sup>2</sup> + 0<sup>2</sup> + 0<sup>2</sup> = 1.
The 143 happy numbers up to 1,000 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 {{OEIS|id=A007770}}.
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 1,000 follow (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.{{OEIS|id=A124095}}.
==Sequence behavior==
If ''n'' is not happy, then its sequence does not go to 1. Instead, it ends in the 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 <math>9^2 m</math>, or <math>81m</math>.
For <math>m=4</math> and above,
:<math>n\geq10^{m-1}>81m</math>
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 times 81, that is, 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 [[Interval (mathematics)|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 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'', 10<sup>n</sup> is happy since its sum is 1
* and for every ''n'', 2 × 10<sup>n</sup> 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. And as in the proof, especially by appending zeroes on the end of the number (by multiplying with 10<sup>n</sup>).
The first pair of consecutive happy numbers is 31, 32.<ref>{{cite web|title=Lower of pair of consecutive happy numbers|url=http://oeis.org/A035502|work=Online Encyclopedia of Integer Sequences|accessdate=8 April 2011}}</ref> The first set of triplets is 1880, 1881, and 1882.<ref>{{cite web|title=First of triples of consecutive happy numbers|url=http://oeis.org/A072494|work=Online Encyclopedia of Integer Sequences|accessdate=8 April 2011}}</ref>
An interesting question is to wonder about the density of happy numbers. In the interval <math>[1,10^{122}]</math>, 15.5% (to three significant figures) are happy.{{OEIS|id=A068571}}
==Happy primes==
A '''happy prime''' is a number that is both happy and [[prime number|prime]]. The happy primes below 500 are <blockquote>7, 13, 19, 23, 31, 79, 97, 103, 109, 139, 167, 193, 239, 263, 293, 313, 331, 367, 379, 383, 397, 409, 487 {{OEIS|id=A035497}}. </blockquote>
All numbers, and therefore all primes, of the form 10<sup>''n''</sup> + 3 or 10<sup>''n''</sup> + 9 for ''n'' greater than 0 are happy (This does not mean that these are the only happy primes, as evidenced by the sequence above). To see this, note that
* All such numbers will have at least two digits;
* The first digit will always be ''1'' due to the 10<sup>n</sup>
* 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: 1<sup>2</sup> + 3<sup>2</sup> = 10 → 1<sup>2</sup> = 1
** The sequence for numbers ending in 9 is: 1<sup>2</sup> + 9<sup>2</sup> = 82 → 8<sup>2</sup> + 2<sup>2</sup> = 64 + 4 = 68 → 6<sup>2</sup> + 8<sup>2</sup> = 36 + 64 = 100 -> 1
The [[palindromic prime]] 10<sup>150006</sup> + 7426247{{e|75000}} + 1 is also a happy prime with 150,007 digits because the many 0's do not contribute to the sum of squared digits, and <math>1^2 + 7^2+4^2+2^2+6^2+2^2+4^2+7^2 + 1^2 = 176</math>, which is a happy number. Paul Jobling discovered the prime in 2005.<ref>[http://primes.utm.edu/primes/page.php?id=76550 The Prime Database: 10^150006+7426247*10^75000+1]</ref>
{{As of|2010}}, the largest known happy prime is <math>2^{42643801}-1</math> ([[Mersenne prime]]). Its decimal expansion has 12,837,064 digits.<ref>[http://primes.utm.edu/primes/page.php?id=88847 Prime Pages entry for 2<sup>42643801</sup> - 1]</ref>
==Happy numbers in other bases==
The definition of happy numbers depends on the decimal (i.e., base 10) representation of the numbers. The definition can be extended to other [[Radix|bases]].
To represent numbers in other bases, we may use a subscript to the right to indicate the base. For instance, <math>100_2</math> represents the number 4, and
:<math>123_5 = 1 \cdot 5^2 + 2 \cdot 5 + 3 =38.</math>
Then, it is easy to see that there are happy numbers in every base. For instance, the numbers
:<math>1_b,10_b,100_b,1000_b,...</math>
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 <math>1000_b</math>. If <math>n < 1000_b</math>, then the sum of the squares of the base-''b'' digits of ''n'' is less than or equal to
:<math>3(b-1)^2</math>
which can be shown to be less than <math>b^3</math>, for <math>b \geq 5</math> . This shows that once the sequence reaches a number less than <math>1000_b</math>, it stays below <math>1000_b</math>, and hence must cycle or reach 1.
In base 2, all numbers are happy. All [[Binary numeral system|binary]] numbers larger than 1000<sub>2</sub> decay into a value equal to or less than 1000<sub>2</sub>, and all such values are happy:
The following four sequences contain all numbers less than <math>1000_2</math>:
:<math> 111_2 \rightarrow 11_2 \rightarrow 10_2 \rightarrow 1 </math>
:<math> 110_2 \rightarrow 10_2 \rightarrow 1 </math>
:<math> 101_2 \rightarrow 10_2 \rightarrow 1 </math>
:<math> 100_2 \rightarrow 1. </math>
Since all sequences end in 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 500,000,000.<ref>{{SloanesRef |sequencenumber=A161872|name=Smallest unhappy number in base n}}</ref>
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 IFF the sum of digits ends in 0 or 2. This is the general application of the test for 9-divisibility in base 10. Recall too that 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's Happy. In this case, the result always end in a one-digit cycle of 0, 1 or 2, repeated infinitely. In [[Ternary numeral system|Unbalanced Ternary]], the digits square to 1 and 4, and in this case there are 5 loops: 0, 1, 2→4→2, 5 and 8. While all even numbers are Sad because they end in the 0, 2 or 8 cycle, some odd numbers are also Sad because they end in 5 or 1, and are thus occasionally sad.{{cn|date=June 2013}}
==Cubing the digits rather than squaring==
A twist 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:
: 1<sup>3</sup>+5<sup>3</sup>+7<sup>3</sup>+9<sup>3</sup>=1+125+343+729=1198
: 1<sup>3</sup>+1<sup>3</sup>+9<sup>3</sup>+8<sup>3</sup>=1+1+729+512=1243
: 1<sup>3</sup>+2<sup>3</sup>+4<sup>3</sup>+3<sup>3</sup>=1+8+64+27=100
: 1<sup>3</sup>+0<sup>3</sup>+0<sup>3</sup>=1
In the same way that when summing the squares of the digits (and working in base 10) each number above 243(=3*81) produces a number which is strictly smaller, when summing the cubes of the digits each number above 2916(=4*729) produces a number which 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 <math> 371 </math> which perpetually produces itself.
those that eventually reach <math> 153 </math> which perpetually produces itself.
those that eventually reach the loop <math> 133 \rightarrow 55 \rightarrow 250 \rightarrow 133 \rightarrow ... </math>
those that eventually reach <math> 370 </math> which perpetually produces itself.
those that eventually reach the loop <math> 217 \rightarrow 352 \rightarrow 160 \rightarrow 217 \rightarrow ... </math>
those that eventually reach <math> 407 </math> which perpetually produces itself.
those that eventually reach the loop <math> 1459 \rightarrow 919 \rightarrow 1459 \rightarrow ... </math>
those that eventually reach the loop <math> 136 \rightarrow 244 \rightarrow 136 \rightarrow ... </math>
Starting with the happy numbers and then following with the unhappy numbers in the order given above, the density of each type of number in the interval [1,2916] is 1.54%, 28.4%, 34.7%, 5.73%, 17.4%, 4.60%, 3.60%, 2.67% and 1.34% (all to three significant figures).
Intriguingly, the second type of unhappy number includes all multiples of three . This fact can be proved by the exhaustive search up to 2916 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 of this type must have a remainder of 1 when dividing by 3.
One interesting result which comes from the above work is that the only positive whole numbers which are the sum of the cubes of their digits are 1, 153, 370, 371 and 407.
==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" {{harvcol|Guy|2004|p=§E34}}.
== Popular culture ==
In the 2007 ''[[Doctor Who]]'' episode "[[42 (Doctor Who)|42]]", a sequence of happy primes (313, 331, 367, 379) is used as a [[code]] for unlocking a sealed door on a spaceship about to collide with a star. When the Doctor learns that nobody on the spaceship besides himself has heard of happy numbers, he asks, "Don't they teach [[recreational mathematics]] anymore?"
The contestants in the 2012 [[University Challenge]] final were asked to identify a sequence of numbers as happy primes in a picture round.
==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 book-keeping (for example, the Python example precomputes the squares of all 10 digits).
* Simple test in [[Python (programming language)|Python]] to check if a number is happy.<ref>[http://rosettacode.org/wiki/Happy_Number Happy Number] Rosetta Code</ref>
<source lang=python>
SQUARE = dict([(c, int(c)**2) for c in "0123456789"])
def is_happy(n):
s = set()
while (n > 1) and (n not in s):
s.add(n)
n = sum(SQUARE[d] for d in str(n))
return n == 1
</source>
==References==
{{reflist}}
===Additional resources===
* Walter Schneider, [http://web.archive.org/web/20060204094653/http://www.wschnei.de/digit-related-numbers/happy-numbers.html Mathews: Happy Numbers].
* {{MathWorld|urlname=HappyNumber|title=Happy Number}}
* [http://mathforum.org/library/drmath/view/55856.html Happy Numbers] at The Math Forum.
* [http://numberphile.com/videos/melancoil.html Numberphile]
* {{Cite book
| last = Guy
| first = Richard
| authorlink = Richard K. Guy
| year = 2004
| title = Unsolved Problems in Number Theory
|edition=3rd
| publisher = [[Springer-Verlag]]
| isbn= 0-387-20860-7
| ref = harv
}}
==External links==
* {{cite web|last=Symonds|first=Ria|title=7 and Happy Numbers|url=http://www.numberphile.com/videos/7happy.html|work=Numberphile|publisher=[[Brady Haran]]}}
{{Use dmy dates|date=June 2011}}
{{Classes of natural numbers}}
{{DEFAULTSORT:Happy Number}}
[[Category:Base-dependent integer sequences]]
[[Category:Recreational mathematics]]' |
New page wikitext, after the edit (new_wikitext ) | '{{distinguish2|[[Harshad number]] (derived from Sanskrit ''harsa'': "great joy")}}
A '''happy number''' is a number defined by the following process: Starting with any [[positive number|positive]] [[integer]], replace the number by the [[sum]] of the squares of its [[numerical digit|digits]], and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which 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''').<ref>{{cite web|url=http://mathworld.wolfram.com/SadNumber.html|title=Sad Number|publisher=Wolfram Research, Inc.|accessdate=2009-09-16}}</ref>
==Overview==
More formally, given a number <math>n=n_0</math>, define a sequence <math>n_1</math>, <math>n_2</math>, ... where <math>n_{i+1}</math> is the sum of the squares of the digits of <math>n_i</math>. Then ''n'' is happy if and only if there exists ''i'' such that <math>n_i = 1</math>.
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:
:1<sup>2</sup> + 9<sup>2</sup> = 82
:8<sup>2</sup> + 2<sup>2</sup> = 68
:6<sup>2</sup> + 8<sup>2</sup> = 100
:1<sup>2</sup> + 0<sup>2</sup> + 0<sup>2</sup> = 1.
The 143 happy numbers up to 1,000 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 {{OEIS|id=A007770}}.
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 1,000 follow (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.{{OEIS|id=A124095}}.
==Sequence behavior==
If ''n'' is not happy, then its sequence does not go to 1. Instead, it ends in the 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 <math>9^2 m</math>, or <math>81m</math>.
For <math>m=4</math> and above,
:<math>n\geq10^{m-1}>81m</math>
so any number over 1000 gets smaller under this process and in particular becomes a number with strictly fewer fuck
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 times 81, that is, 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 [[Interval (mathematics)|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 starts 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 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 numberd would be a fixed point of the described process.
There are infinitely many happy number and infinitely many unhappy numbers. Consider the following proof:
* 1 is a happy number, and for every ''n'', 10<sup>n</sup> is happy since its sum is 1
* and for every ''n'', 2 × 10<sup>n</sup> 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 crosse sum. And as in the proof, especially by appending zeroes on the end of the number (by multiplying with 10<sup>n</sup>).
The first pair of consecutive happy numbers is 31, 32.<ref>{{cite web|title=Lower of pair of consecutive happy numbers|url=http://oeis.org/A035502|work=Online Encyclopedia of Integer Sequences|accessdate=8 April 2011}}</ref> The first set of triplets is 1880, 1881, and 1882.<ref>{{cite web|title=First of triples of consecutive happy numbers|url=http://oeis.org/A072494|work=Online Encyclopedia of Integer Sequences|accessdate=8 April 2011}}</ref>
An interesting question is to wonder about the density of happy numbers. In the interval <math>[1,10^{122}]</math>, 15.5% (to three significant figures) are happy.{{OEIS|id=A068571}}
==Happy primes==
A '''happy prime''' is a number that is both happy and [[prime number|prime]]. The happy primes below 500 are <blockquote>7, 13, 19, 23, 31, 79, 97, 103, 109, 139, 167, 193, 239, 263, 293, 313, 331, 367, 379, 383, 397, 409, 487 {{OEIS|id=A035497}}. </blockquote>
All numbers, and therefore all primes, of the form 10<sup>''n''</sup> + 3 or 10<sup>''n''</sup> + 9 for ''n'' greater than 0 are happy (This does not mean that these are the only happy primes, as evidenced by the sequence above). To see this, note that
* All such numbers will have at least two digits;
* The first digit will always be ''1'' due to the 10<sup>n</sup>
* 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: 1<sup>2</sup> + 3<sup>2</sup> = 10 → 1<sup>2</sup> = 1
** The sequence for numbers ending in 9 is: 1<sup>2</sup> + 9<sup>2</sup> = 82 → 8<sup>2</sup> + 2<sup>2</sup> = 64 + 4 = 68 → 6<sup>2</sup> + 8<sup>2</sup> = 36 + 64 = 100 -> 1
The [[palindromic prime]] 10<sup>150006</sup> + 7426247{{e|75000}} + 1 is also a happy prime with 150,007 digits because the many 0's do not contribute to the sum of squared digits, and <math>1^2 + 7^2+4^2+2^2+6^2+2^2+4^2+7^2 + 1^2 = 176</math>, which is a happy number. Paul Jobling discovered the prime in 2005.<ref>[http://primes.utm.edu/primes/page.php?id=76550 The Prime Database: 10^150006+7426247*10^75000+1]</ref>
{{As of|2010}}, the largest known happy prime is <math>2^{42643801}-1</math> ([[Mersenne prime]]). Its decimal expansion has 12,837,064 digits.<ref>[http://primes.utm.edu/primes/page.php?id=88847 Prime Pages entry for 2<sup>42643801</sup> - 1]</ref>
==Happy numbers in other bases==
The definition of happy numbers depends on the decimal (i.e., base 10) representation of the numbers. The definition can be extended to other [[Radix|bases]].
To represent numbers in other bases, we may use a subscript to the right to indicate the base. For instance, <math>100_2</math> represents the number 4, and
:<math>123_5 = 1 \cdot 5^2 + 2 \cdot 5 + 3 =38.</math>
Then, it is easy to see that there are happy numbers in every base. For instance, the numbers
:<math>1_b,10_b,100_b,1000_b,...</math>
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 <math>1000_b</math>. If <math>n < 1000_b</math>, then the sum of the squares of the base-''b'' digits of ''n'' is less than or equal to
:<math>3(b-1)^2</math>
which can be shown to be less than <math>b^3</math>, for <math>b \geq 5</math> . This shows that once the sequence reaches a number less than <math>1000_b</math>, it stays below <math>1000_b</math>, and hence must cycle or reach 1.
In base 2, all numbers are happy. All [[Binary numeral system|binary]] numbers larger than 1000<sub>2</sub> decay into a value equal to or less than 1000<sub>2</sub>, and all such values are happy:
The following four sequences contain all numbers less than <math>1000_2</math>:
:<math> 111_2 \rightarrow 11_2 \rightarrow 10_2 \rightarrow 1 </math>
:<math> 110_2 \rightarrow 10_2 \rightarrow 1 </math>
:<math> 101_2 \rightarrow 10_2 \rightarrow 1 </math>
:<math> 100_2 \rightarrow 1. </math>
Since all sequences end in 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 500,000,000.<ref>{{SloanesRef |sequencenumber=A161872|name=Smallest unhappy number in base n}}</ref>
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 IFF the sum of digits ends in 0 or 2. This is the general application of the test for 9-divisibility in base 10. Recall too that 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's Happy. In this case, the result always end in a one-digit cycle of 0, 1 or 2, repeated infinitely. In [[Ternary numeral system|Unbalanced Ternary]], the digits square to 1 and 4, and in this case there are 5 loops: 0, 1, 2→4→2, 5 and 8. While all even numbers are Sad because they end in the 0, 2 or 8 cycle, some odd numbers are also Sad because they end in 5 or 1, and are thus occasionally sad.{{cn|date=June 2013}}
==Cubing the digits rather than squaring==
A twist 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:
: 1<sup>3</sup>+5<sup>3</sup>+7<sup>3</sup>+9<sup>3</sup>=1+125+343+729=1198
: 1<sup>3</sup>+1<sup>3</sup>+9<sup>3</sup>+8<sup>3</sup>=1+1+729+512=1243
: 1<sup>3</sup>+2<sup>3</sup>+4<sup>3</sup>+3<sup>3</sup>=1+8+64+27=100
: 1<sup>3</sup>+0<sup>3</sup>+0<sup>3</sup>=1
In the same way that when summing the squares of the digits (and working in base 10) each number above 243(=3*81) produces a number which is strictly smaller, when summing the cubes of the digits each number above 2916(=4*729) produces a number which 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 <math> 371 </math> which perpetually produces itself.
those that eventually reach <math> 153 </math> which perpetually produces itself.
those that eventually reach the loop <math> 133 \rightarrow 55 \rightarrow 250 \rightarrow 133 \rightarrow ... </math>
those that eventually reach <math> 370 </math> which perpetually produces itself.
those that eventually reach the loop <math> 217 \rightarrow 352 \rightarrow 160 \rightarrow 217 \rightarrow ... </math>
those that eventually reach <math> 407 </math> which perpetually produces itself.
those that eventually reach the loop <math> 1459 \rightarrow 919 \rightarrow 1459 \rightarrow ... </math>
those that eventually reach the loop <math> 136 \rightarrow 244 \rightarrow 136 \rightarrow ... </math>
Starting with the happy numbers and then following with the unhappy numbers in the order given above, the density of each type of number in the interval [1,2916] is 1.54%, 28.4%, 34.7%, 5.73%, 17.4%, 4.60%, 3.60%, 2.67% and 1.34% (all to three significant figures).
Intriguingly, the second type of unhappy number includes all multiples of three . This fact can be proved by the exhaustive search up to 2916 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 of this type must have a remainder of 1 when dividing by 3.
One interesting result which comes from the above work is that the only positive whole numbers which are the sum of the cubes of their digits are 1, 153, 370, 371 and 407.
==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" {{harvcol|Guy|2004|p=§E34}}.
== Popular culture ==
In the 2007 ''[[Doctor Who]]'' episode "[[42 (Doctor Who)|42]]", a sequence of happy primes (313, 331, 367, 379) is used as a [[code]] for unlocking a sealed door on a spaceship about to collide with a star. When the Doctor learns that nobody on the spaceship besides himself has heard of happy numbers, he asks, "Don't they teach [[recreational mathematics]] anymore?"
The contestants in the 2012 [[University Challenge]] final were asked to identify a sequence of numbers as happy primes in a picture round.
==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 book-keeping (for example, the Python example precomputes the squares of all 10 digits).
* Simple test in [[Python (programming language)|Python]] to check if a number is happy.<ref>[http://rosettacode.org/wiki/Happy_Number Happy Number] Rosetta Code</ref>
<source lang=python>
SQUARE = dict([(c, int(c)**2) for c in "0123456789"])
def is_happy(n):
s = set()
while (n > 1) and (n not in s):
s.add(n)
n = sum(SQUARE[d] for d in str(n))
return n == 1
</source>
==References==
{{reflist}}
===Additional resources===
* Walter Schneider, [http://web.archive.org/web/20060204094653/http://www.wschnei.de/digit-related-numbers/happy-numbers.html Mathews: Happy Numbers].
* {{MathWorld|urlname=HappyNumber|title=Happy Number}}
* [http://mathforum.org/library/drmath/view/55856.html Happy Numbers] at The Math Forum.
* [http://numberphile.com/videos/melancoil.html Numberphile]
* {{Cite book
| last = Guy
| first = Richard
| authorlink = Richard K. Guy
| year = 2004
| title = Unsolved Problems in Number Theory
|edition=3rd
| publisher = [[Springer-Verlag]]
| isbn= 0-387-20860-7
| ref = harv
}}
==External links==
* {{cite web|last=Symonds|first=Ria|title=7 and Happy Numbers|url=http://www.numberphile.com/videos/7happy.html|work=Numberphile|publisher=[[Brady Haran]]}}
{{Use dmy dates|date=June 2011}}
{{Classes of natural numbers}}
{{DEFAULTSORT:Happy Number}}
[[Category:Base-dependent integer sequences]]
[[Category:Recreational mathematics]]' |