In number theory, a happy number is a number which eventually reaches 1 when 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. A number which is not happy is called sad or unhappy.
More generally, a -happy number is a natural number in a given number base that eventually reaches 1 when iterated over the perfect digital invariant function for . [1]
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).
Formally, let be a natural number. Given the perfect digital invariant function
for base , a number is -happy if there exists a such that , where represents the -th iteration of , and -unhappy otherwise. If a number is a nontrivial 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. The happiness 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 a natural 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. [2]
A happy base is a number base where every number is -happy. The only happy integer bases less than 5×108 are base 2 and base 4. [3]
For , the only positive perfect digital invariant for is the trivial perfect digital invariant 1, and there are no other cycles. Because all numbers are preperiodic 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
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
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 distinct combinations of digits that form 10-happy numbers below 1000 are (the rest are just rearrangements and/or insertions of zero digits):
The first pair of consecutive 10-happy numbers is 31 and 32. [4] The first set of three consecutive is 1880, 1881, and 1882. [5] It has been proven that there exist sequences of consecutive happy numbers of any natural number length. [6] The beginning of the first run of at least n consecutive 10-happy numbers for n = 1, 2, 3, ... is [7]
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." [8]
The number of 10-happy numbers up to 10n for 1 ≤ n ≤ 20 is [9]
A -happy prime is a number that is both -happy and prime. 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, as base 2 and base 4 are happy bases.
In base 6, the 6-happy primes below 1296 = 64 are
In base 10, the 10-happy primes below 500 are
The palindromic prime 10150006 + 7426247×1075000 + 1 is a 10-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 10-happy number. Paul Jobling discovered the prime in 2005. [10]
As of 2010 [update] , the largest known 10-happy prime is 242643801 − 1 (a Mersenne prime).[ dubious – discuss ] Its decimal expansion has 12837064 digits. [11]
In base 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, and repeating a number.
A simple test in Python 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
19 (nineteen) is the natural number following 18 and preceding 20. It is a prime number.
21 (twenty-one) is the natural number following 20 and preceding 22.
23 (twenty-three) is the natural number following 22 and preceding 24.
81 (eighty-one) is the natural number following 80 and preceding 82.
73 (seventy-three) is the natural number following 72 and preceding 74. In English, it is the smallest natural number with twelve letters in its spelled out name.
31 (thirty-one) is the natural number following 30 and preceding 32. It is a prime number.
61 (sixty-one) is the natural number following 60 and preceding 62.
1000 or one thousand is the natural number following 999 and preceding 1001. In most English-speaking countries, it can be written with or without a comma or sometimes a period separating the thousands digit: 1,000.
300 is the natural number following 299 and preceding 301.
400 is the natural number following 399 and preceding 401.
700 is the natural number following 699 and preceding 701.
900 is the natural number following 899 and preceding 901. It is the square of 30 and the sum of Euler's totient function for the first 54 positive integers. In base 10, it is a Harshad number. It is also the first number to be the square of a sphenic number.
3000 is the natural number following 2999 and preceding 3001. It is the smallest number requiring thirteen letters in English.
6000 is the natural number following 5999 and preceding 6001.
8000 is the natural number following 7999 and preceding 8001.
181 is the natural number following 180 and preceding 182.
10,000,000 is the natural number following 9,999,999 and preceding 10,000,001.
In mathematics, a pandigital number is an integer that in a given base has among its significant digits each digit used in the base at least once. For example, 1234567890 is a pandigital number in base 10.
A sum-product number in a given number base is a natural number that is equal to the product of the sum of its digits and the product of its digits.
In number theory and mathematical logic, a Meertens number in a given number base is a natural number that is its own Gödel number. It was named after Lambert Meertens by Richard S. Bird as a present during the celebration of his 25 years at the CWI, Amsterdam.