Replace a positive integer with the sum of the squares of its digits, and repeat. If the chain never reaches 1, the number is an unhappy number. It gets stuck in a loop that always passes through 4.
Four clear steps — then stop at 4 or use a visited array.
Require n > 0 (positive integers only).
Define f(n) = sum of squares of the digits of n.
Keep applying f until the value is 1 (happy) or you detect a loop (unhappy).
Return true for unhappy: the chain hit 4, or it repeated a value without reaching 1.
Method
Idea
Extra space
Best for
Stop at 4
Loop until n is 1 or 4
O(1)
Short code, interviews
Visited array
Stop when a value repeats
O(k)
No need to know the cycle
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
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Core step
$total += ($n % 10) ** 2; $n = intdiv($n, 10);
Unhappy when
Chain never reaches 1 (it hits 4 or repeats)
Happy when
Chain reaches 1
Known cycle
4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4
Shortcut
while ($n !== 1 && $n !== 4) { $n = f($n); }
Classic unhappy
2, 3, 4, 5, 6, 8, 9, 11
Classic happy
1, 7, 10, 13, 19, 23
Try it
Live Preview
Enter a positive integer to see the digit-square chain and the unhappy/happy verdict.
Positive whole numbers only. Tap a chip or type a value.
Start with the short stop-at-4 check. Then use a visited array when you do not want to rely on the known cycle.
Example 1 — Stop at 4
Every unhappy number reaches 4, so stop there and return true.
php
<?php
function sumOfSquares($n) {
$total = 0;
while ($n > 0) {
$d = $n % 10;
$total += $d * $d;
$n = intdiv($n, 10);
}
return $total;
}
function isUnhappy($n) {
if ($n <= 0) {
return false;
}
while ($n !== 1 && $n !== 4) {
$n = sumOfSquares($n);
}
return $n === 4;
}
$number = 2;
if (isUnhappy($number)) {
echo $number . " is an Unhappy Number.\n";
} else {
echo $number . " is not an Unhappy Number.\n";
}
?>
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.
Keep replacing n until it becomes 1 (happy) or 4 (inside the unhappy cycle).
For 2, the chain is 2 → 4, so the loop stops at 4.
return $n === 4; gives true for unhappy numbers and false for happy ones.
Example 2 — Visited Array
Store every value you produce as an array key. Reach 1 → happy; see a repeat first → unhappy.
php
<?php
function sumOfSquares($n) {
$total = 0;
while ($n > 0) {
$d = $n % 10;
$total += $d * $d;
$n = intdiv($n, 10);
}
return $total;
}
function isUnhappy($n) {
if ($n <= 0) {
return false;
}
$seen = [];
while ($n !== 1 && !isset($seen[$n])) {
$seen[$n] = true;
$n = sumOfSquares($n);
}
return $n !== 1;
}
echo isUnhappy(2) ? "2 is unhappy\n" : "2 is happy\n";
echo isUnhappy(4) ? "4 is unhappy\n" : "4 is happy\n";
echo isUnhappy(7) ? "7 is unhappy\n" : "7 is happy\n";
echo isUnhappy(19) ? "19 is unhappy\n" : "19 is happy\n";
?>
Output
2 is unhappy
4 is unhappy
7 is happy
19 is happy
How It Works
Reuse the same sumOfSquares() helper as Example 1.
Add each value to $seen before computing the next one.
If $n becomes 1, the loop ends and $n !== 1 is false — happy.
If $n is already in $seen, the chain is looping without reaching 1, so $n !== 1 is true — unhappy.
This works even if you forget the 4-cycle, at the cost of a little extra memory.
Edge Cases & Pitfalls
Check these before calling the solution done.
1
Not unhappy
1 is happy, so isUnhappy(1) must be false.
Cycle
Must stop
Without a stop at 4 or a visited array, unhappy inputs loop forever.
0 / negatives
Out of scope
The definition is for positive integers. Reject the rest.
Squares
Not digit sum
You square each digit. Plain digit sum is a different problem.
Analysis
Time and Space Complexity
Program
Time
Extra space
Stop at 4
O(k · log n)
O(1)
Visited array
O(k · log n)
O(k)
Each square-sum step costs O(digits). Values shrink quickly into a small range, so k (steps until 1 or a repeat) stays small in practice.
Remember
Key Takeaways
Definition: keep summing digit squares; if you never reach 1, the number is unhappy.
Stop safely: stop at 4 or use a visited array so unhappy inputs do not loop forever.
Return value:isUnhappy() is true when the chain hits 4.
Test set: 2 and 4 unhappy; 1, 7, and 19 happy.
One line: sum digit squares until you hit 1 (happy) or 4 (unhappy).
Frequently Asked Questions
A positive integer that never reaches 1 when you repeatedly replace it with the sum of the squares of its digits. It ends up looping in the cycle 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4.
A number that does reach 1 under the same digit-square process. Every positive integer is either happy or unhappy.
Every unhappy number eventually lands in the cycle that contains 4. So if the process hits 4, you can stop and answer unhappy without storing visited values.
Yes. 2 → 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4, and it never reaches 1.
No. 1 is already happy because it equals 1. Unhappy numbers never reach 1.
The definition uses positive integers. Reject n <= 0 before running the process.