Happy Numbers: Can a Number Make You Happy?
Take the number 7. Square each digit and add the results. Then do it again, and again:
7 → 7² = 49
49 → 4² + 9² = 16 + 81 = 97
97 → 9² + 7² = 81 + 49 = 130
130 → 1² + 3² + 0² = 10
10 → 1² + 0² = 1
It reaches 1 and stays there forever (1² = 1). That makes 7 a happy number.
Now try 4. It never reaches 1. Instead it gets trapped in a loop. And here's the surprise: every number that isn't happy falls into the very same loop.
There Are Only Two Possible Fates
You might expect a process like this to wander unpredictably, sometimes growing forever, sometimes producing all sorts of different cycles. It doesn't.
Starting from any positive whole number, the sequence of digit-square sums always ends in exactly one of two ways:
- It reaches 1 (happy)
- It enters the cycle 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 (unhappy)
No third possibility exists. And it's not hard to prove.
The Unhappy Loop
Let's follow 4:
4 → 16
16 → 1 + 36 = 37
37 → 9 + 49 = 58
58 → 25 + 64 = 89
89 → 64 + 81 = 145
145 → 1 + 16 + 25 = 42
42 → 16 + 4 = 20
20 → 4 + 0 = 4 ← back to the start
Eight numbers chase each other forever. Any unhappy number eventually lands somewhere on this loop. See 145 and 89, two of its members.
Why Numbers Can't Grow Forever
The key is that big numbers shrink under this process.
The largest digit square is 9² = 81. So a number with d digits produces a sum of at most 81d. Compare:
| Digits (d) | Smallest d-digit number | Largest possible sum (81d) |
|---|---|---|
| 3 | 100 | 243 |
| 4 | 1,000 | 324 |
| 5 | 10,000 | 405 |
| 10 | 1,000,000,000 | 810 |
From 4 digits on, the sum is always smaller than the number itself. So any starting number keeps shrinking until it's below 1,000. And for 3-digit numbers, the sum is at most 243.
Once the sequence is trapped among a finite set of values (at most 243 possible numbers), it must eventually repeat a value, by the pigeonhole principle. And once a value repeats, the sequence cycles forever.
So every sequence ends in a cycle. Checking all numbers up to 243 by computer shows there are only two cycles: the fixed point 1 and the eight-number loop. That completes the proof.
The First Happy Numbers
The happy numbers up to 100 are:
1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100
That's 20 out of 100. Up to 1,000 there are 143. A few patterns:
- Rearranging digits doesn't change happiness: 13, 31 and 103 are all happy, since the digit squares are the same.
- Inserting zeros doesn't either: 7, 70 and 700 are all happy.
- If n is happy, so is every number n leads to. 49, 97, 130 and 10 are happy because 7 is.
Happy Primes
Numbers that are both happy and prime are happy primes:
7, 13, 19, 23, 31, 79, 97, 103, 109, 139, 167, 193, …
They had a moment of pop-culture fame in the 2007 Doctor Who episode "42," in which the Doctor has to identify the next number in the sequence 313, 331, 367, … The answer is 379, and the Doctor explains they're happy primes. Check one with the prime checker for 379.
An Insider Reference: Where the Name Came From
Happy numbers entered the mathematical literature through Richard Guy's classic book Unsolved Problems in Number Theory. Guy credited mathematician Reg Allenby, whose daughter had learned about them at school in the United Kingdom. Nobody seems to know which teacher first gave them the cheerful name.
There's still an unsolved question hiding here. What fraction of numbers are happy? Among numbers up to 100 it's 20%, and up to 1,000 about 14%. But in 2013, Justin Gilmer proved that the proportion doesn't settle down to a single limit. As you count higher, the fraction keeps swinging, with an upper density of at least 18.5% and a lower density of at most 11.4%.
Changing the Rules
The idea generalizes:
- Other bases: In base 2, every number is happy. In base 10, most aren't.
- Cubes instead of squares: Summing cubes of digits leads to different fixed points, including 153, 370, 371 and 407, the 3-digit narcissistic numbers.
Two Concepts Worth Knowing
Pigeonhole Principle
If you put more items into boxes than there are boxes, some box must hold two items. Applied to sequences, a process confined to finitely many values must eventually repeat, which guarantees a cycle.
Iteration
Iteration means applying the same function again and again to its own output. Iterated functions produce fixed points, cycles and sometimes chaos. Happy numbers are one of the gentlest examples.
Quick Answer: What Is a Happy Number?
A happy number eventually reaches 1 when you repeatedly replace it with the sum of the squares of its digits. For example, 7 → 49 → 97 → 130 → 10 → 1. Unhappy numbers instead fall into the cycle 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4.
Try Them Yourself
- Number 7: the smallest happy number after 1
- Number 145: part of the unhappy cycle
- Square Numbers List: digit squares at a glance
- Is 379 Prime?: the Doctor Who happy prime
- Addition Flash Cards: practice adding digit squares quickly
- Narcissistic Numbers: what happens with cubes
Test the year you were born. Is it happy? If not, which number in the 8-cycle does it land on first?