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 array 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 visited array, or use Floyd cycle detection for O(1) extra space.
Method
Idea
Extra space
Best for
Visited array
Stop when a value repeats
O(1) fixed map
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 array so the stop condition is obvious. Then use Floyd’s cycle detection when you want pure O(1) pointers.
Example 1 — Visited Array
Mark every value you produce. Reach 1 → happy; see a repeat → unhappy.
C
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int sum_of_squares(int n)
{
int total = 0;
while (n > 0)
{
int d = n % 10;
total += d * d;
n /= 10;
}
return total;
}
bool is_happy(int n)
{
/* After one step, values fit well under 1000 for normal ints. */
bool seen[1000];
if (n <= 0)
{
return false;
}
memset(seen, 0, sizeof(seen));
while (n != 1)
{
if (n < 1000 && seen[n])
{
return false;
}
if (n < 1000)
{
seen[n] = true;
}
n = sum_of_squares(n);
}
return true;
}
int main(void)
{
int number = 19;
if (is_happy(number))
{
printf("%d is a Happy Number.\\n", number);
}
else
{
printf("%d is not a Happy Number.\\n", number);
}
return 0;
}
Output
19 is a Happy Number.
How It Works
sum_of_squares peels digits with % 10 and adds each square.
Reject n <= 0 so only positive integers are checked.
While n is not 1, mark it in seen (once it is under 1000).
If that value appears again, you hit a cycle — return false.
If n becomes 1, return true.
Example 2 — Floyd Cycle Detection
Move a slow pointer one step and a fast pointer two steps. Meeting at 1 means happy.
C
#include <stdio.h>
#include <stdbool.h>
int sum_of_squares(int n)
{
int total = 0;
while (n > 0)
{
int d = n % 10;
total += d * d;
n /= 10;
}
return total;
}
bool is_happy(int n)
{
int slow;
int fast;
if (n <= 0)
{
return false;
}
slow = n;
fast = sum_of_squares(n);
while (fast != 1 && slow != fast)
{
slow = sum_of_squares(slow);
fast = sum_of_squares(sum_of_squares(fast));
}
return fast == 1;
}
int main(void)
{
printf("%s\\n", is_happy(19) ? "19 is happy" : "19 is unhappy");
printf("%s\\n", is_happy(2) ? "2 is happy" : "2 is unhappy");
printf("%s\\n", is_happy(7) ? "7 is happy" : "7 is unhappy");
return 0;
}
Output
19 is happy
2 is unhappy
7 is happy
How It Works
Reuse the same sum_of_squares helper as the visited 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 array method, but with only two integer pointers.
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 visited map 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 array
O(k · log n)
O(1) fixed map
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.