A happy number is a positive integer that eventually reaches \(1\) when it is repeatedly replaced by the sum of the squares of its decimal digits.
If the process enters a repeating cycle without reaching \(1\), the number is called unhappy.
1. Digit-square-sum transformation
The happy-number test uses a transformation \(f(n)\). If the decimal digits of \(n\) are \(d_1,d_2,\ldots,d_k\), then:
\[
\begin{aligned}
f(n)
&=
d_1^2+d_2^2+\cdots+d_k^2.
\end{aligned}
\]
In words, square every digit and add the squares.
Example:
\[
\begin{aligned}
f(19)
&=1^2+9^2\\
&=1+81\\
&=82.
\end{aligned}
\]
2. Definition of a happy number
Starting with a positive integer \(n\), repeatedly apply \(f\):
\[
\begin{aligned}
n,\quad f(n),\quad f(f(n)),\quad f(f(f(n))),\ldots
\end{aligned}
\]
If this sequence reaches \(1\), then \(n\) is happy.
\[
\begin{aligned}
n\text{ is happy}
\quad\Longleftrightarrow\quad
f^{(m)}(n)=1
\text{ for some }m\ge0.
\end{aligned}
\]
3. Definition of an unhappy number
If the sequence never reaches \(1\) and instead repeats a previous value, it enters a cycle.
Then the starting number is unhappy.
\[
\begin{aligned}
n\text{ is unhappy}
\quad\Longleftrightarrow\quad
\text{the sequence repeats before reaching }1.
\end{aligned}
\]
4. Worked example: \(19\)
Start with \(19\):
\[
\begin{aligned}
19
&\to 1^2+9^2=82,\\
82
&\to 8^2+2^2=68,\\
68
&\to 6^2+8^2=100,\\
100
&\to 1^2+0^2+0^2=1.
\end{aligned}
\]
The sequence is:
\[
\begin{aligned}
19\to82\to68\to100\to1.
\end{aligned}
\]
Since the sequence reaches \(1\), \(19\) is a happy number.
5. Worked example: \(4\)
Start with \(4\):
\[
\begin{aligned}
4
&\to 4^2=16,\\
16
&\to 1^2+6^2=37,\\
37
&\to 3^2+7^2=58,\\
58
&\to 5^2+8^2=89,\\
89
&\to 8^2+9^2=145,\\
145
&\to 1^2+4^2+5^2=42,\\
42
&\to 4^2+2^2=20,\\
20
&\to 2^2+0^2=4.
\end{aligned}
\]
Now \(4\) has appeared again. Therefore the sequence will repeat:
\[
\begin{aligned}
4\to16\to37\to58\to89\to145\to42\to20\to4\to\cdots
\end{aligned}
\]
Since this cycle does not contain \(1\), \(4\) is unhappy.
6. Why the process eventually stops or cycles
For a number with \(k\) decimal digits, the largest possible digit-square sum is:
\[
\begin{aligned}
9^2+9^2+\cdots+9^2
&=81k.
\end{aligned}
\]
This is much smaller than most large \(k\)-digit numbers. After a few transformations, the sequence enters a bounded range.
Once only finitely many values are possible, one of two things must happen:
- the sequence reaches \(1\), so the number is happy;
- the sequence repeats a previous value, so it enters a cycle and the number is unhappy.
7. Cycle detection
The calculator stores every value it has already seen. At each step:
- If the next value is \(1\), the number is happy.
- If the next value has already appeared, the number is unhappy.
- Otherwise, continue applying the transformation.
This is reliable because a repeated value means the same future sequence will repeat forever.
8. The common unhappy cycle
In base \(10\), unhappy numbers eventually enter the well-known cycle:
\[
\begin{aligned}
4\to16\to37\to58\to89\to145\to42\to20\to4.
\end{aligned}
\]
A number that falls into this loop is unhappy because the loop never reaches \(1\).
9. Step-by-step method
- Write the decimal digits of the number.
- Square each digit.
- Add the squares.
- Repeat the process with the new value.
- Stop when the sequence reaches \(1\) or repeats a previous value.
10. Examples
The table below uses plain text formulas to avoid raw LaTeX appearing inside table cells.
| Starting number |
Sequence |
Classification |
| 1 |
1 |
Happy |
| 7 |
7 → 49 → 97 → 130 → 10 → 1 |
Happy |
| 10 |
10 → 1 |
Happy |
| 19 |
19 → 82 → 68 → 100 → 1 |
Happy |
| 4 |
4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 |
Unhappy |
| 20 |
20 → 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 |
Unhappy |
11. Formula summary
The formula table uses plain text in table cells for reliable rendering across templates.
12. Common mistakes
- Adding the digits instead of adding the squares of the digits.
- Stopping too early before the sequence reaches \(1\) or repeats.
- Thinking a large intermediate value means the number cannot be happy.
- Forgetting that \(1\) itself is already happy.
- Not checking for cycles when the sequence does not quickly reach \(1\).
Key idea: keep applying the digit-square-sum rule. Reaching \(1\) means happy; repeating means unhappy.