A positive integer is an unhappy number if repeatedly replacing it with the sum of the squares of its digitsnever reaches 1. Instead the chain falls into a loop. It is the complement of a happy number.
Remember
n is unhappy ⇔ digit-square chain never reaches 1
Every unhappy chain ends in this cycle:
4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4
Example: 2
2² = 4
4² = 16
1² + 6² = 37 ... and so on, back to 4
n
Chain (short)
Result
2
2 → 4 → 16 → … (cycle)
Unhappy
4
already in the cycle
Unhappy
20
20 → 4 → (cycle)
Unhappy
19
19 → 82 → 68 → 100 → 1
Happy
Approach
How to Check an Unhappy Number in Java
Reuse the happy-number helper, then flip the answer. Pick a visited set or the stop-at-4 shortcut.
Require n > 0 (positive integers only).
Define f(n) = sum of squares of the digits of n.
Keep applying f until you reach 1 (happy) or detect a loop (unhappy).
Report unhappy when the chain never reaches 1.
Method
Idea
Extra space
Best for
Visited set
Unhappy when a value repeats before 1
O(k)
Clarity, first solution
Stop at 4
Unhappy when the chain hits 4 (known cycle)
O(1)
Short code, interviews
Pseudocode
Pseudocode
function SumOfSquares(n):
total = 0
while n > 0:
d = n mod 10
total = total + d * d
n = n / 10
return total
function IsUnhappy(n):
if n <= 0:
return false
while n != 1 and n != 4:
n = SumOfSquares(n)
return n == 4
Start with a visited set so the stop condition is obvious. Then use the stop-at-4 shortcut and list unhappy numbers in a range.
Example 1 — Visited HashSet
Store every value you produce. Reach 1 → happy; see a repeat first → unhappy.
Java
import java.util.HashSet;
import java.util.Set;
public class UnhappyNumber {
static int sumOfSquares(int n) {
int total = 0;
while (n > 0) {
int d = n % 10;
total += d * d;
n /= 10;
}
return total;
}
static boolean isUnhappy(int n) {
if (n <= 0) {
return false;
}
Set<Integer> seen = new HashSet<>();
while (n != 1 && !seen.contains(n)) {
seen.add(n);
n = sumOfSquares(n);
}
return n != 1;
}
public static void main(String[] args) {
int number = 2;
if (isUnhappy(number)) {
System.out.println(number + " is an Unhappy Number.");
} else {
System.out.println(number + " is not an Unhappy Number.");
}
}
}
Output
2 is an Unhappy Number.
How It Works
sumOfSquares peels digits with % 10 and adds each square.
Reject n <= 0 so only positive integers are checked.
While n is not 1 and has not been seen, add it to seen and replace it with sumOfSquares(n).
The loop ends either at 1 (happy) or on a repeated value (a cycle).
Return n != 1: leaving the loop without reaching 1 means the number is unhappy.
Example 2 — Stop at 4 and List a Range
Every unhappy chain reaches 4, so you can stop there without a set. Then print the unhappy numbers from 1 to 20.
Java
public class UnhappyRange {
static int sumOfSquares(int n) {
int total = 0;
while (n > 0) {
int d = n % 10;
total += d * d;
n /= 10;
}
return total;
}
static boolean isUnhappy(int n) {
if (n <= 0) {
return false;
}
while (n != 1 && n != 4) {
n = sumOfSquares(n);
}
return n == 4;
}
public static void main(String[] args) {
System.out.println("Unhappy numbers from 1 to 20:");
for (int i = 1; i <= 20; i++) {
if (isUnhappy(i)) {
System.out.print(i + " ");
}
}
}
}
Shortcut: loop until n == 1 or n == 4; return n == 4.
Test set: 2, 3, 4, 20 unhappy; 1, 7, 19 happy.
One line: sum digit squares — if you land in the 4-cycle instead of 1, the number is unhappy.
Frequently Asked Questions
A positive integer is unhappy if repeatedly replacing it with the sum of the squares of its digits never reaches 1. The chain loops forever instead.
Every unhappy number eventually enters the cycle 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4. That is why spotting a 4 is enough to call a number unhappy.
No. 1 is happy because it already equals 1. Unhappy numbers never reach 1.
4 belongs to the only cycle that unhappy numbers can enter, so once the chain hits 4 it is guaranteed never to reach 1. This avoids storing every visited value.
They are complements. Happy numbers reach 1; unhappy numbers do not. You can reuse the same digit-square helper and flip the final answer.
The usual definition uses positive integers. Reject nonpositive inputs, and do not report them as unhappy.