Back to Math Tools

Happy Number Calculator

Check if a number is happy by repeatedly summing the squares of its digits until reaching 1 or entering a cycle.

Enter a Positive Integer

Enter any positive integer to check if it is a happy number

How It Works

A happy number is defined by the following iterative process: starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat until the number equals 1 (where it will stay), or it loops endlessly in a cycle that does not include 1.

Algorithm:

  1. Take a positive integer n
  2. Replace n with the sum of the squares of its digits
  3. If n = 1, the original number is happy
  4. If n enters a cycle (seen before), the original number is unhappy
  5. Otherwise, repeat from step 2

The Unhappy Cycle:

4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4

All unhappy numbers eventually enter this 8-number cycle.

Example (Happy): For n = 19:

  • 1² + 9² = 1 + 81 = 82
  • 8² + 2² = 64 + 4 = 68
  • 6² + 8² = 36 + 64 = 100
  • 1² + 0² + 0² = 1 → Happy!

Example (Unhappy): For n = 2:

  • 2² = 4
  • 4² = 16
  • 1² + 6² = 37
  • ... enters the cycle → Unhappy

Interesting Properties: If a number is happy, then any permutation of its digits is also happy. For example, 19 and 91 are both happy. Also, adding zeros to a happy number keeps it happy (19, 109, 1009 are all happy).

Frequently Asked Questions

What is a happy number?

A happy number is a positive integer that eventually reaches 1 when you repeatedly replace it with the sum of the squares of its digits. For example, 19 is happy: 1² + 9² = 82, then 8² + 2² = 68, then 6² + 8² = 100, then 1² + 0² + 0² = 1. Numbers that don't reach 1 are called unhappy or sad numbers.

What happens to unhappy numbers?

Unhappy numbers eventually enter an infinite cycle that includes the numbers 4, 16, 37, 58, 89, 145, 42, and 20. Once a number enters this cycle, it will never reach 1. For example, starting with 2: 2 → 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 (cycle repeats).

How many happy numbers are there?

There are infinitely many happy numbers. The happy numbers under 100 are: 1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, and 100. Interestingly, if a number is happy, then any permutation of its digits is also happy (e.g., 19, 91 are both happy).

What is the mathematical significance of happy numbers?

Happy numbers are studied in recreational mathematics and number theory. They demonstrate interesting properties of digit-based operations and cycle detection algorithms. The concept was introduced by Reg Allenby in 1949. Happy numbers also appear in programming interviews as a classic example of cycle detection using Floyd's algorithm.