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 HashSet, 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 = 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) * (n % 10); 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
Trace
Worked Walkthrough — n = 19
Square each digit, sum, and replace until you hit 1.
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 HashSet
Store every value you produce. Reach 1 → happy; see a repeat → unhappy.
C#
using System;
using System.Collections.Generic;
class Program
{
static int SumOfSquares(int n)
{
int total = 0;
while (n > 0)
{
int d = n % 10;
total += d * d;
n /= 10;
}
return total;
}
static bool IsHappy(int n)
{
if (n <= 0)
{
return false;
}
HashSet<int> seen = new HashSet<int>();
while (n != 1 && !seen.Contains(n))
{
seen.Add(n);
n = SumOfSquares(n);
}
return n == 1;
}
static void Main()
{
int number = 19;
if (IsHappy(number))
{
Console.WriteLine(number + " is a Happy Number.");
}
else
{
Console.WriteLine(number + " is not a Happy Number.");
}
}
}
Output
19 is a Happy 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 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.
C#
using System;
class Program
{
static int SumOfSquares(int n)
{
int total = 0;
while (n > 0)
{
int d = n % 10;
total += d * d;
n /= 10;
}
return total;
}
static bool IsHappy(int n)
{
if (n <= 0)
{
return false;
}
int slow = n;
int fast = SumOfSquares(n);
while (fast != 1 && slow != fast)
{
slow = SumOfSquares(slow);
fast = SumOfSquares(SumOfSquares(fast));
}
return fast == 1;
}
static void Main()
{
Console.WriteLine(IsHappy(19) ? "19 is happy" : "19 is unhappy");
Console.WriteLine(IsHappy(2) ? "2 is happy" : "2 is unhappy");
Console.WriteLine(IsHappy(7) ? "7 is happy" : "7 is unhappy");
}
}
Output
19 is happy
2 is unhappy
7 is happy
How It Works
Reuse the same SumOfSquares helper as the set version.
slow advances one application of f; fast advances two.
If fast hits 1, the number is happy — stop.
If slow == fast before that, both landed in the unhappy cycle.
Same answers as the set method, but with constant extra memory.
Edge Cases & Pitfalls
Check these before calling the solution done.
1
Already happy
1 is happy immediately. Do not force an extra step.
Cycle
Must detect loops
Without a set or Floyd, unhappy inputs loop forever.
0 / negatives
Out of scope
Happy numbers are defined 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
Visited HashSet
O(k · log n)
O(k)
Floyd
O(k · log n)
O(1)
Each square-sum step costs O(digits). Values shrink quickly into a small range, so k (chain length until 1 or a cycle) stays small in practice.