Numbers with a certain property involving recursive summation
Not to be confused withHarshad number (derived from Sanskritharśa meaning "great joy").
Tree showing all happy numbers up to 100, and 130Sad numbers up to 100 showing the repeating cycle of 8 numbers
Innumber theory, ahappy number is a number which eventually reaches 1 when the number is replaced by the sum of the square of each digit. For instance, 13 is a happy number because, and. On the other hand, 4 is not a happy number because the sequence starting with and eventually reaches, the number that started the sequence, and so the process continues in an infinite cycle without ever reaching 1.[1] A number which is not happy is calledsad orunhappy.
The origin of happy numbers is not clear. Happy numbers were brought to the attention of Reginald Allenby (a British author and senior lecturer inpure mathematics atLeeds University) by his daughter, who had learned of them at school. However, they "may have originated in Russia."[3]
for base, a number is-happy if there exists a such that, where represents the-thiteration of, and-unhappy otherwise. If a number is anontrivial perfect digital invariant of, then it is-unhappy.
For example, 19 is 10-happy, as
For example, 347 is 6-happy, as
There are infinitely many-happy numbers, as 1 is a-happy number, and for every, ( in base) is-happy, since its sum is 1. Thehappiness of a number is preserved by removing or inserting zeroes at will, since they do not contribute to the cross sum.
By inspection of the first million or so 10-happy numbers, it appears that they have anatural 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.[4]
For, the only positive perfect digital invariant for is the trivial perfect digital invariant 1, and there are no other cycles. Because all numbers arepreperiodic points for, all numbers lead to 1 and are happy. As a result,base 4 is a happy base.
For, the only positive perfect digital invariant for is the trivial perfect digital invariant 1, and the only cycle is the eight-number cycle
5 → 41 → 25 → 45 → 105 → 42 → 32 → 21 → 5 → ...
and because all numbers are preperiodic points for, all numbers either lead to 1 and are happy, or lead to the cycle and are unhappy. Because base 6 has no other perfect digital invariants except for 1, no positive integer other than 1 is the sum of the squares of its own digits.
In base 10, the 74 6-happy numbers up to 1296 = 64 are (written in base 10):
For, the only positive perfect digital invariant for is the trivial perfect digital invariant 1, and the only cycle is the eight-number cycle
4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 → ...
and because all numbers are preperiodic points for, all numbers either lead to 1 and are happy, or lead to the cycle and are unhappy. Because base 10 has no other perfect digital invariants except for 1, no positive integer other than 1 is the sum of the squares of its own digits.
In base 10, the 143 10-happy numbers up to 1000 are:
The first pair of consecutive 10-happy numbers is 31 and 32.[6] The first set of three consecutive is 1880, 1881, and 1882.[7] It has been proven that there exist sequences of consecutive happy numbers of any natural number length.[8] The beginning of the first run of at leastn consecutive 10-happy numbers forn = 1, 2, 3, ... is[9]
As Robert Styer puts it in his paper calculating this series: "Amazingly, the same value of N that begins the least sequence of six consecutive happy numbers also begins the least sequence of seven consecutive happy numbers."[10]
The number of 10-happy numbers up to 10n for 1 ≤ n ≤ 20 is[11]
A-happy prime is a number that is both-happy andprime. Unlike happy numbers, rearranging the digits of a-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, asbase 2 andbase 4 are happy bases.
Thepalindromic prime10150006 +7426247×1075000 + 1 is a 10-happy prime with150007 digits because the many 0s do not contribute to the sum of squared digits, and12 + 72 + 42 + 22 + 62 + 22 + 42 + 72 + 12 = 176, which is a 10-happy number. Paul Jobling discovered the prime in 2005.[12]
Inbase 12, there are no 12-happy primes less than 10000, the first 12-happy primes are (the letters X and E represent the decimal numbers 10 and 11 respectively)
The examples below implement the perfect digital invariant function for and a default base 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, andrepeating a number.
A simple test inPython to check if a number is happy:
defpdi_function(number,base:int=10):"""Perfect digital invariant function."""total=0whilenumber>0:total+=pow(number%base,2)number=number//basereturntotaldefis_happy(number:int)->bool:"""Determine if the specified number is a happy number."""seen_numbers=set()whilenumber>1andnumbernotinseen_numbers:seen_numbers.add(number)number=pdi_function(number)returnnumber==1
In 2007, the concept of happy numbers was used inProfessor Layton and the Diabolical Box, in puzzle 149 ("Number Cycle"), using the sequence beginning with 4, which repeats every 8 terms.
In theDoctor Who episode42, a sequence of happy primes is the password to open a door.
^Honsberger, Ross (1970).Ingenuity in mathematics. New mathematical library. New York: The Mathematical association of America. pp. 83–84.ISBN978-0-88385-623-9.
^"Sad Number". Wolfram Research, Inc. Retrieved16 September 2009.
^Guy, Richard K. (13 July 2004).Unsolved problems in number theory. Problem books in mathematics (3rd ed.). New York: Springer. pp. 357–358.ISBN978-0-387-20860-2.