A happy number is a positive integer that, when replaced by the sum of the squares of its digits repeatedly, eventually reaches the number 1. If this process results in an endless cycle containing the number 4, then the number is called an unhappy number.
A number is called a happy number if, after replacing the number by the sum of the squares of its digits, the process is repeated until the number becomes 1.
For example, the number 32 is a happy number because the process yields 1 as follows:
\(3^2 + 2^2 = 13\)
\(1^2 + 3^2 = 10\)
\(1^2 + 0^2 = 1\)
Let \(n\) be the original number, and \(d_1, d_2, ..., d_k\) be its digits. The sum of the squares of its
digits is given by:
\(n = d_1^2 + d_2^2 + ... + d_k^2\)
1. Start with a positive integer.
2. Replace the number by the sum of the squares of its digits.
3. Repeat the process until the number becomes 1 (happy) or an endless cycle containing 4 (unhappy).
Check Single Happy Number
Check Happy Numbers in Range