Readablewiki

Happy number

Content sourced from Wikipedia, licensed under CC BY-SA 3.0.

Happy numbers are numbers that become 1 when you repeatedly replace the number by the sum of the squares of its digits. If the process never reaches 1 and instead falls into a loop, the number is sad or unhappy.

Example:
- 13 → 1^2 + 3^2 = 10, and 10 → 1^2 + 0^2 = 1. So 13 is happy.
- 4 follows a loop: 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4, never reaching 1. So 4 is unhappy.

In any base b, you can define a b-happy number the same way, using the digits in base b. 1 is b-happy in every base. Some bases have the special property that every number is b-happy (happy bases). For example, base 4 is a happy base, so all numbers in base 4 eventually reach 1.

In base 10, there is a famous eight-number cycle that unhappy numbers can fall into: 4, 16, 37, 58, 89, 145, 42, 20, and back to 4. Numbers that reach 1 are happy, while those that fall into this cycle are unhappy. There are infinitely many happy numbers in base 10, and many primes can also be happy in a given base (happy primes).

How to check if a number is happy:
- Repeatedly replace the number by the sum of the squares of its digits.
- Stop when you reach 1 (happy) or when you see a number you’ve already seen (unhappy, since you’re in a loop).

A couple of quick examples:
- 19 is happy in base 10: 1^2 + 9^2 = 82, then 8^2 + 2^2 = 68, then 6^2 + 8^2 = 100, then 1^2 + 0^2 + 0^2 = 1.
- 31 and 32 are the first pair of consecutive happy numbers in base 10.


This page was last edited on 3 February 2026, at 00:06 (CET).