Loading…

Happy or Unhappy Numbers

Math Algebra • Numbers

View all topics

Determine whether a positive integer is a happy number. Repeatedly replace the number by the sum of the squares of its digits. If the sequence reaches \(1\), the number is happy. If the sequence repeats without reaching \(1\), it is unhappy.

Transform: square each digit and add Happy: sequence reaches 1 Unhappy: sequence enters a cycle Example: 19 → 82 → 68 → 100 → 1

Number input

Supported range: \(1\le n\le 10^9\). The calculator stores previous values to detect a cycle. A repeated value means the process will loop forever unless it has already reached \(1\).

Quick examples

Ready
Enter a positive integer, then click “Calculate”.

Rate this calculator

0.0 /5 (0 ratings)
Be the first to rate.
Your rating
You can update your rating any time.

Frequently Asked Questions

What is a happy number?

A happy number is a positive integer that eventually reaches 1 when repeatedly replaced by the sum of the squares of its digits.

What is an unhappy number?

An unhappy number is a positive integer whose digit-square-sum sequence repeats before reaching 1, so it enters a cycle.

How does the happy number test work?

Square each digit of the number, add the squares, and repeat the process with the new value until the sequence reaches 1 or repeats.

Is 19 a happy number?

Yes. The sequence is 19 -> 82 -> 68 -> 100 -> 1, so 19 is happy.

Is 4 a happy number?

No. The sequence enters the cycle 4 -> 16 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20 -> 4, so 4 is unhappy.

Is 1 a happy number?

Yes. The number 1 is already the stopping value, so it is happy with zero transformations.

Why does a repeated value mean unhappy?

Once a value repeats, the same future values will repeat forever. If 1 was not reached before the repeat, the number is unhappy.

Why does the process eventually reach 1 or repeat?

For a k-digit number, the maximum digit-square sum is 81k, so after a few steps the sequence becomes bounded. A bounded sequence with finitely many possible values must eventually reach 1 or repeat.

What is the common unhappy cycle?

In base 10, unhappy numbers eventually fall into the cycle 4 -> 16 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20 -> 4.

Does the calculator show the full sequence?

Yes. It shows the full sequence, the number of transformations, and the cycle if one is detected.