A positive integer is a happy number if repeatedly replacing it with the sum of the squares of its digits eventually reaches 1. If the process loops without hitting 1, the number is unhappy.
Four clear steps — then pick a visited set or Floyd’s tortoise and hare.
Require n > 0 (positive integers only).
Define f(n) = sum of squares of digits of n.
Keep applying f until you reach 1 (happy) or detect a repeat (unhappy).
Detect repeats with a Set, or use Floyd cycle detection for O(1) extra space.
Method
Idea
Extra space
Best for
Visited set
Stop when a value repeats
O(k)
Clarity, first solution
Floyd
slow = f(slow), fast = f(f(fast))
O(1)
Interviews, memory limits
Pseudocode
Pseudocode
function SumOfSquares(n):
total = 0
while n > 0:
d = n mod 10
total = total + d * d
n = floor(n / 10)
return total
function IsHappy(n):
if n <= 0:
return false
seen = empty set
while n != 1 and n not in seen:
add n to seen
n = SumOfSquares(n)
return n == 1
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Core step
total += (n % 10) ** 2; n = Math.floor(n / 10)
Happy when
Chain reaches 1
Unhappy when
A value repeats before 1
Known cycle
4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4
Floyd move
slow = f(slow); fast = f(f(fast))
Classic happy
1, 7, 10, 13, 19, 23
Classic unhappy
2, 4, 5, 16
Try it
Live Preview
Enter a positive integer to see the digit-square chain and the happy/unhappy verdict.
Positive whole numbers only. Tap a chip or type a value.
Result
n = 19
Chain: 19 → 82 → 68 → 100 → 1
Verdict: HAPPY NUMBER
Code
JavaScript Programs
Start with a visited set so the stop condition is obvious. Then use Floyd’s cycle detection when you want O(1) extra space.
Example 1 — Visited Set
Store every value you produce. Reach 1 → happy; see a repeat → unhappy.
JavaScript
function sumOfSquares(n) {
let total = 0;
while (n > 0) {
const d = n % 10;
total += d * d;
n = Math.floor(n / 10);
}
return total;
}
function isHappy(n) {
if (n <= 0) {
return false;
}
const seen = new Set();
while (n !== 1 && !seen.has(n)) {
seen.add(n);
n = sumOfSquares(n);
}
return n === 1;
}
const number = 19;
if (isHappy(number)) {
console.log(number + " is a Happy Number.");
} else {
console.log(number + " is not a Happy Number.");
}
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 with sumOfSquares(n).
If n becomes 1, return true.
If n reappears in seen, you hit a cycle — return false.
Example 2 — Floyd Cycle Detection
Move a slow pointer one step and a fast pointer two steps. Meeting at 1 means happy.
JavaScript
function sumOfSquares(n) {
let total = 0;
while (n > 0) {
const d = n % 10;
total += d * d;
n = Math.floor(n / 10);
}
return total;
}
function isHappy(n) {
if (n <= 0) {
return false;
}
let slow = n;
let fast = sumOfSquares(n);
while (fast !== 1 && slow !== fast) {
slow = sumOfSquares(slow);
fast = sumOfSquares(sumOfSquares(fast));
}
return fast === 1;
}
console.log(isHappy(19) ? "19 is happy" : "19 is unhappy");
console.log(isHappy(2) ? "2 is happy" : "2 is unhappy");
console.log(isHappy(7) ? "7 is happy" : "7 is unhappy");