Mathwords logoMathwords

Happy Number — Definition, Formula & Examples

A happy number is a positive integer that eventually reaches 1 when you replace it over and over with the sum of the squares of its digits. For example, 7 is a happy number because the process 7 → 49 → 97 → 130 → 10 → 1 terminates at 1.

Define a function S(n)S(n) that maps a positive integer nn to the sum of the squares of its digits. A positive integer nn is called a happy number if there exists some k0k \geq 0 such that Sk(n)=1S^k(n) = 1, where SkS^k denotes the kk-th iterate of SS. If the sequence n,S(n),S2(n),n, S(n), S^2(n), \ldots never reaches 1, then nn is called an unhappy (or sad) number.

Key Formula

S(n)=i=0d1ai2S(n) = \sum_{i=0}^{d-1} a_i^{\,2}
Where:
  • nn = The positive integer being tested
  • aia_i = The $i$-th digit of $n$
  • dd = The number of digits in $n$

How It Works

To test whether a number is happy, take each digit, square it, and add up all the squares. Then repeat the process on the result. If you eventually land on 1, the number is happy. If instead you enter a cycle that never includes 1, the number is unhappy. A key fact: every unhappy number eventually enters the cycle 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4. So as soon as you see 4 appear, you can stop — the number is not happy.

Worked Example

Problem: Determine whether 23 is a happy number.
Step 1: Square each digit of 23 and add the results.
22+32=4+9=132^2 + 3^2 = 4 + 9 = 13
Step 2: Repeat with 13.
12+32=1+9=101^2 + 3^2 = 1 + 9 = 10
Step 3: Repeat with 10.
12+02=1+0=11^2 + 0^2 = 1 + 0 = 1
Step 4: We reached 1, so the process terminates.
Answer: 23 is a happy number because the sequence 23 → 13 → 10 → 1 reaches 1.

Another Example

Problem: Determine whether 5 is a happy number or an unhappy number.
Step 1: Start with 5.
52=255^2 = 25
Step 2: Process 25.
22+52=4+25=292^2 + 5^2 = 4 + 25 = 29
Step 3: Process 29.
22+92=4+81=852^2 + 9^2 = 4 + 81 = 85
Step 4: Process 85.
82+52=64+25=898^2 + 5^2 = 64 + 25 = 89
Step 5: We hit 89, which is part of the known unhappy cycle (89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89 → ...). We can stop here.
Answer: 5 is an unhappy number because its sequence enters a repeating cycle and never reaches 1.

Visualization

Why It Matters

Happy numbers appear as a popular exercise in computer science courses and coding interviews because testing them requires loops, cycle detection, and set operations. They also connect to genuine number theory: researchers study the density of happy numbers among all integers and their behavior in different bases. Exploring happy numbers builds skills in systematic iteration that transfer to topics like modular arithmetic and fixed-point analysis.

Common Mistakes

Mistake: Forgetting to square the digits — just summing the digits instead of summing their squares.
Correction: Each digit must be squared before you add them. For 23, compute 22+32=132^2 + 3^2 = 13, not 2+3=52 + 3 = 5.
Mistake: Assuming the process must reach 1 quickly and giving up after a few steps.
Correction: Some happy numbers take many iterations. Keep going, or watch for the number 4 to confirm the number is unhappy.

Related Terms