Happy Number Calculator

Happy number verdictHappy
Replacements to the verdict4
Final value1
How this was calculated
  1. A happy number reaches 1 when you repeatedly replace it by the sum of the squares of its digits; an unhappy one falls into a loop that never reaches 1.
  2. Follow the chain: 19 -> 82 -> 68 -> 100 -> 1.
  3. It reaches 1 after 4 replacements, so 19 is a happy number - 19 and 7 are the classic small examples.

A happy number calculator tells you whether a whole number is happy or unhappy by applying one simple rule over and over: replace the number with the sum of the squares of its digits. If the chain eventually reaches 1, the number is happy. If it instead falls into a loop that never reaches 1, the number is unhappy. The calculator runs those replacements for you and reports the answer, so you do not have to square and add digits by hand. For example, 19 is happy: 19 becomes 82, then 68, then 100, then 1, in 4 replacements. The number 7 is happy too, in 5 replacements: 7, 49, 97, 130, 10, 1. The number 20 is not happy: its chain loops back to 20 after 8 replacements and never reaches 1.

The method behind the calculator is the accepted standard for this kind of calculation because it follows the definition of a happy number exactly, with no shortcuts or guesswork. Every unhappy number falls into the same loop, passing through 4, 16, 37, 58, 89, 145, 42 and 20, so a chain that enters that loop can never reach 1. This page shows the formula, the step-by-step workings, worked examples and sources, so you can check each result yourself rather than trusting a black box.

Worked examples

The classic 19

19 is Happy: 19 -> 82 -> 68 -> 100 -> 1, in 4 replacements. (19)

Happy number verdictHappy
Replacements to the verdict4
Final value1

The lucky 7

7 is Happy too, in 5 replacements. (7)

Happy number verdictHappy
Replacements to the verdict5
Final value1

The unhappy 20

20 is Not happy: the chain loops back to 20 after 8 replacements. (20)

Happy number verdictNot happy
Replacements to the verdict8
Final value20

What a happy number is

A happy number is defined by a simple process. Take any positive whole number, replace it with the sum of the squares of its digits, and repeat that step on each new result. If the chain eventually lands on 1, the original number is happy. If it never reaches 1, the number is unhappy. That is the whole rule, and a happy number calculator just runs this digit-square-sum iteration for you and reports where the chain ends up.

The chain for 19 shows how it works. Square each digit and add: 1² + 9² = 1 + 81 = 82. Repeat on 82: 8² + 2² = 64 + 4 = 68. Then 68 gives 6² + 8² = 36 + 64 = 100, and 100 gives 1² + 0² + 0² = 1. So 19 is happy, and it takes 4 replacements to get there. The number 7 is happy too, in 5 replacements: 7 -> 49 -> 97 -> 130 -> 10 -> 1.

Not every number ends at 1. Take 20: the chain loops back to 20 after 8 replacements and never reaches 1. That makes 20 unhappy. In fact, every unhappy number falls into the same loop, passing through 4, 16, 37, 58, 89, 145, 42 and 20 before returning to the start. Because the loop is fixed, an unhappy number can never escape it, no matter how long you keep iterating.

This gives you a practical way to check any number by hand. Work through the digit squares step by step, and watch for two outcomes: a 1, which means the number is happy, or any entry in the unhappy loop, which means it is not. The loop members are the reliable warning signs, so once you see 4, 16, 37, 58, 89, 145, 42 or 20 appear, you can stop and call the number unhappy.

  • 19 is happy: 19 -> 82 -> 68 -> 100 -> 1, in 4 replacements.
  • 7 is happy: 7 -> 49 -> 97 -> 130 -> 10 -> 1, in 5 replacements.
  • 20 is not happy: its chain loops back to 20 after 8 replacements and never reaches 1.
  • Every unhappy number falls into the same loop, through 4, 16, 37, 58, 89, 145, 42 and 20.
Quick check: keep replacing the number by the sum of the squares of its digits. If you hit 1, it is happy. If you hit 4, 16, 37, 58, 89, 145, 42 or 20, it is unhappy.

Worked examples

The clearest way to see how a happy number calculator works is to run a few numbers by hand. Take 19, a classic happy number. Square each digit and add: 1² + 9² = 1 + 81 = 82. Repeat with 82: 8² + 2² = 64 + 4 = 68. Then 68 gives 6² + 8² = 36 + 64 = 100. Finally, 100 gives 1² + 0² + 0² = 1. The chain is 19 → 82 → 68 → 100 → 1, and it reaches 1 after 4 replacements. Because it lands on 1, 19 is happy.

Now try 7, which is happy as well but takes a little longer. Start with 7² = 49. Next, 4² + 9² = 16 + 81 = 97. Then 9² + 7² = 81 + 49 = 130. Then 1² + 3² + 0² = 1 + 9 + 0 = 10. And 1² + 0² = 1. The full chain is 7 → 49 → 97 → 130 → 10 → 1, reaching 1 in 5 replacements. Counting the steps carefully is where a happy number calculator earns its keep: one skipped link and the count is wrong.

Not every number ends at 1. Consider 20. Squaring its digits gives 2² + 0² = 4. From there: 4² = 16; 1² + 6² = 1 + 36 = 37; 3² + 7² = 9 + 49 = 58; 5² + 8² = 25 + 64 = 89; 8² + 9² = 64 + 81 = 145; 1² + 4² + 5² = 1 + 16 + 25 = 42; and 4² + 2² = 16 + 4 = 20. After 8 replacements the chain is back where it started, at 20, and it never reaches 1. That makes 20 unhappy.

The 20 example also shows the shared fate of every unhappy number. All of them fall into the same loop, cycling through 4, 16, 37, 58, 89, 145, 42 and 20 before returning to the start. So when you test a number, there are only two possible outcomes: the chain hits 1, or it enters that loop. Watching for the loop is what lets a calculator stop and declare a number unhappy instead of running forever.

  • 19 → 82 → 68 → 100 → 1: happy, 4 replacements.
  • 7 → 49 → 97 → 130 → 10 → 1: happy, 5 replacements.
  • 20 → 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20: unhappy, loops back after 8 replacements.
  • The unhappy loop runs through 4, 16, 37, 58, 89, 145, 42 and 20.
Every unhappy number ends up in the same loop, through 4, 16, 37, 58, 89, 145, 42 and 20 — so a chain either reaches 1 or repeats that cycle.

The unhappy loop

A happy number reaches 1 when you repeatedly replace it by the sum of the squares of its digits. An unhappy one never gets there. Instead, its chain of sums eventually settles into a repeating loop, and the same loop every time: 4, 16, 37, 58, 89, 145, 42, 20, and then back to 4. If a number's chain ever lands on any of those eight values, it can never reach 1, because the loop just keeps cycling. That is why a happy number calculator can stop early and say "unhappy" the moment it sees one of these eight numbers appear.

Take 20 as an example. Squaring its digits gives 4, then 16, then 37, then 58, then 89, then 145, then 42, and then 20 again — the chain loops back to where it started after 8 replacements. From that point the same eight numbers repeat forever, so 1 will never show up. The loop is a closed circuit: each number in it produces the next, and 20 produces 4, which closes the circle.

Compare that with the happy cases. 19 is happy: 19 -> 82 -> 68 -> 100 -> 1, in 4 replacements. 7 is happy too, in 5 replacements: 7 -> 49 -> 97 -> 130 -> 10 -> 1. These chains wander for a few steps and then land on 1, where the process stops, since 1 squared is 1 and the sum stays at 1. So every starting number ends in one of exactly two places: the fixed point at 1, or the eight-number loop.

The existence of a single shared loop is what makes the test finite. You never have to check forever. Either the chain reaches 1, or it reaches one of the eight loop values — 4, 16, 37, 58, 89, 145, 42 or 20 — and once you see any of them you know the answer is no. In practice, a happy number calculator just keeps squaring and summing digits, watching for 1 or for a member of the loop, and it can report a result after a short run of steps rather than an endless one.

Every unhappy number shares one loop: 4 -> 16 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20 -> 4. Spot any of these eight numbers in a chain and you know it will never reach 1.

Happy numbers in mathematics

Happy numbers are not a rare curiosity confined to puzzles. In base 10, roughly 1 in 7 positive integers is happy — a density of about 14 percent — so a happy number calculator will usually find a match within the first handful of numbers you try. The property is defined by a simple process: replace the number by the sum of the squares of its digits, and repeat. If the chain reaches 1, the number is happy. If it never does, the number is unhappy.

The examples make the process concrete. 19 is happy: 19 -> 82 -> 68 -> 100 -> 1, in 4 replacements. 7 is happy too, in 5 replacements: 7 -> 49 -> 97 -> 130 -> 10 -> 1. 20 is not happy: its chain loops back to 20 after 8 replacements and never reaches 1. What makes the unhappy case so tidy is that every unhappy number falls into the same loop, through 4, 16, 37, 58, 89, 145, 42 and 20. There is only one trap, and every unhappy chain eventually lands in it.

Because the rule depends on digits, it depends on the base you write them in. A number happy in base 10 need not be happy in another base: the same quantity has different digits elsewhere, so the sums of squares differ and the chains can end differently. The density changes with the base as well, so a figure like 14 percent describes base 10 only. When you use a happy number calculator, check which base it assumes before trusting a verdict on a number written in another base.

A happy prime is a number that is both prime and happy. 7 and 19 are both examples, since each is prime and each reaches 1 in the chains above. Happy primes are a natural meeting point of two unrelated-looking ideas — primality, which ignores how a number is written, and happiness, which depends entirely on its digits — and they are a common reason people reach for the tool.

Every unhappy number ends in the same loop: 4, 16, 37, 58, 89, 145, 42, 20 — and back to 4.

FAQs

What is a happy number?

A happy number is a number that reaches 1 when you repeatedly replace it with the sum of the squares of its digits. The classic example is 19: square each digit, add the results, and keep going until you either reach 1 or fall into a loop. If you reach 1, the number is happy.

How do you test if a number is happy?

Square each digit, add them, and repeat with the result. For 19, the steps are 19 -> 82 -> 68 -> 100 -> 1. Because the process ends at 1, 19 is happy. You can run the same steps on any number to test it.

What happens if a number is not happy?

If a number is not happy, it falls into a loop that never reaches 1. For example, 20 loops back to 20 after 8 replacements. Once the sequence returns to a value it already hit, it will repeat forever, so the number can never be happy.

Is 7 a happy number?

Yes, 7 is a happy number. Following the digit-square rule, 7 -> 49 -> 97 -> 130 -> 10 -> 1, which takes 5 replacements. Since the sequence ends at 1, 7 is happy.

What is the unhappy loop?

Every unhappy number ends in the same cycle: 4, 16, 37, 58, 89, 145, 42, 20, and then it repeats. Once a number's sequence enters this cycle, it will keep going around it and never reach 1, which is why the number is unhappy.

Are most numbers happy or unhappy?

Most numbers are unhappy. Roughly four out of five numbers below 1,000 never reach 1, meaning they fall into the repeating loop instead. So if you test a random number under 1,000, it is more likely to be unhappy than happy.

Sources

Related calculators

Armstrong Number Calculator
Check whether a number is an Armstrong (narcissistic) number: the sum of its digits each raised to the power of the digit count, compared with the number itself.
Perfect Number Calculator
Classify a number as perfect, abundant or deficient by comparing it with the sum of its proper divisors (its aliquot sum).
Prime Number Calculator
Primality by trial division, the full prime factorization of composites, and the neighbouring primes on either side.