In number theory, a happy number is defined as a number that, when repeatedly subjected to the process of squaring its digits and summing those squares, eventually leads to 1. An unhappy number will never reach 1 during this process, and will get stuck in an infinite loop [1].
Given an integer, determine if it's a happy number.
Input: n = 23
Output: True
Explanation: + = 13 ⇒ + = 10 ⇒ + = 1
New to this one? Start here. Below is the core idea in everyday language, plus a real-world analogy—then the sections that follow build on it with the formal reasoning, code, and step-by-step walkthroughs.
Each number is a door, and its digit-square-sum tells you which door to walk through next. Some paths lead to the exit (the door labelled 1, which just loops on itself). Other paths wander in circles forever. You want to know: does this starting door lead to the exit, or am I doomed to loop? Since there is no map, send a slow walker and a fast walker — if the fast one reaches the exit, you're happy; if it catches the slow one first, you're stuck in a loop.
We can simulate the process of identifying a happy number by repeatedly summing the squares of each digit of a number, and then applying the same process to the resulting sum.
According to the problem statement, this process could conclude in one of two ways:
If we diagram both scenarios, we observe something interesting:
This looks quite similar to a linked list problem. In particular, the problem of determining if a linked list has a cycle (case 2) or doesn’t (case 1).
We can reduce this problem to the same cycle detection challenge as the Linked List Loop problem. By applying the fast and slow pointer technique (i.e., Floyd's Cycle Detection algorithm), we can efficiently determine if a cycle exists.
However, in this problem, we don't have an actual linked list to perform the fast and slow pointer algorithm on. Therefore, we need to find a way to traverse the sequence of numbers generated in the happy number process.
Conveniently, we already know what the “next” number in the sequence is for any number x. As described in the problem statement, the next number can be calculated by summing the square of each digit of x. So, if each number were a node in a linked list, we could get the “next” node by calculating the next number in the sequence.
Getting the next number in the sequence
To calculate the next number of x, we need a way to access each digit of x. This can be done in two steps:
The modulo operation (x % 10) is used to extract the last digit of a number x.
Divide x by 10 (x = x / 10) to truncate the last digit, positioning the next digit as the new last digit.
We can see this unfold in full below for x = 123:
Now that we have a way to traverse the sequence, we can implement Floyd’s Cycle Detection algorithm. To start, set the fast and slow pointers at the start of this sequence. Then move the pointers as follows:
Advance the slow pointer one number at a time (slow = get_next_num(slow)).
Advance the fast pointer two numbers at a time
(fast = get_next_num(get_next_num(fast))).
If the fast and slow pointers meet during the process, it indicates the presence of a cycle, meaning the number is not a happy number. Otherwise, the algorithm will end when we reach 1, in which case the number is a happy number.
def happy_number(n: int) -> bool:
slow = fast = n
while True:
slow = get_next_num(slow)
fast = get_next_num(get_next_num(fast))
if fast == 1:
return True
# If the fast and slow pointers meet, a cycle is detected. Hence, 'n' is not
# a happy number.
elif fast == slow:
return False
def get_next_num(x: int) -> int:
next_num = 0
while x > 0:
# Extract the last digit of 'x'.
digit = x % 10
# Truncate (remove) the last digit from 'x' using floor division.
x //= 10
# Add the square of the extracted digit to the sum.
next_num += digit ** 2
return next_numint nextNum(int x) { // sum of squares of digits
int total = 0;
while (x > 0) {
int d = x % 10;
total += d * d;
x /= 10;
}
return total;
}
bool happyNumber(int n) {
int slow = n;
int fast = nextNum(n);
while (fast != 1 && slow != fast) {
slow = nextNum(slow); // one step
fast = nextNum(nextNum(fast)); // two steps
}
return fast == 1;
}private int nextNum(int x) { // sum of squares of digits
int total = 0;
while (x > 0) {
int d = x % 10;
total += d * d;
x /= 10;
}
return total;
}
public boolean happyNumber(int n) {
int slow = n;
int fast = nextNum(n);
while (fast != 1 && slow != fast) {
slow = nextNum(slow); // one step
fast = nextNum(nextNum(fast)); // two steps
}
return fast == 1;
}Time complexity: The time complexity of happy_number is . The full analysis of this time complexity is quite complicated and beyond the scope of interviews. For interested readers, please see the detailed analysis below.
Space complexity: The space complexity is .
Two step-by-step walkthroughs of the algorithm below: a valid case (the expected happy path) and an invalid / edge case that exercises the tricky parts. Each step shows the program state as the code runs—the same steps apply to Python, C++ and Java.
slow (+1) fast (+2)
| iteration | slow | fast | fast == 1? |
|---|---|---|---|
| start | 19 | 82 | no |
| 1 | 82 | 100 | no |
| 2 | 68 | 1 | yes → return true |
Row 2: slow = next(82) = 68; fast = next(next(100)) = next(1) = 1. The loop's fast != 1 test now fails, so we stop and return fast == 1 → true.
slow (+1) fast (+2) the repeating cycle
n = 2 never hits 1 — it falls into an 8-number cycle (4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → back to 4):
| iteration | slow | fast | slow == fast? |
|---|---|---|---|
| start | 2 | 4 | no |
| 1 | 4 | 37 | no |
| 2 | 16 | 89 | no |
| 3 | 37 | 42 | no |
| 4 | 58 | 4 | no |
| 5 | 89 | 37 | no |
| 6 | 145 | 89 | no |
| 7 | 42 | 42 | yes (and ≠ 1) → return false |
Contrast with Dry run 1: a happy number's hare marches into the fixed point 1, so the fast != 1 test ends the loop and we return true. An unhappy number has no 1 in its cycle, so the hare instead laps the tortoise — slow == fast at 42 — and since that value isn't 1 we return false. Same collision logic as Linked List Loop.
Both runs step exactly as the tables above. Python starts fast = next_num(n) (one ahead) and loops while fast != 1 and slow != fast, returning fast == 1.
Same steps. C++: int fast = nextNum(n); loop while (fast != 1 && slow != fast); return fast == 1.
Same steps. Java: int fast = nextNum(n); loop while (fast != 1 && slow != fast); return fast == 1.
Tip: Visualize the problem.
At first glance, this problem seems like it requires mathematical reasoning to solve. However, when we visualized the problem, we were able to formulate a solution using an algorithm we already know (Floyd’s Cycle Detection). Visualizing a problem can help uncover hidden patterns or data structures that can lead to the solution.
The following time complexity analysis establishes an upper bound on the steps required to determine a happy number. In this analysis, we define the "next number" of a number as the result obtained by summing the squares of the digits of .
1) Upper limit for the next number For any number with a fixed number of digits, the maximum value for its successor is achieved when all its digits are 9. For instance, the maximum next number from a 3-digit number happens when this 3-digit number is 999.
2) Size of the next number relative to the number of digits
If a number has 1 or 2 digits, it’s possible for the next number to be larger than (e.g., the next number of 99 is 162, which is one digit longer).
If a number has 3 or more digits, the next number is always smaller than the original value of . The table below highlights how the largest number with 3 or more digits has a smaller next number.
| Digits | Largest Number | Next Number |
|---|---|---|
| 1 | 9 | 81 |
| 2 | 99 | 162 |
| 3 | 999 | 243 |
| 4 | 9999 | 324 |
| 5 | 99999 | 405 |
| 6 | 999999 | 486 |
| ... | ... | ... |
3) Implications for cycles Since the next number is always smaller for numbers with three or more digits, it means a cycle can only commence in the happy number process once the number falls below 243 (the next number of 999). This is because we’ve observed that any number larger than 243 will have the next number smaller than . However, once we fall below 243, the next number can potentially be larger, potentially cycling back to a previous number.
4) Time complexity for numbers less than 243 Once a number falls below 243, the algorithm will take less than 243 steps to either converge to 1 or to cycle back to a previous number in the sequence. Therefore, since the length of the cycle or the number of steps to reach 1 is bounded by 243, the time complexity of Floyd’s cycle detection algorithm for numbers less than 243 is .
5) Time complexity for numbers greater than 243
The number of digits in a number is approximately equal to (base 10). So, the calculation of the next number of will take approximately steps (i.e., get_next_num will take steps to execute).
Let's call ’s next number . The next number after () will take approximately steps to calculate. The next number after () will take approximately steps to calculate, and so on. From this, we can summarize the time complexity of this process as . Since we've established that where is the last number greater than 243, the dominant component of this time complexity is . So, the time complexity for numbers greater than 243 is .
Conclusion When is less than 243, the time complexity is O(1), and when is greater than 243, the time complexity is . Therefore, the overall time complexity of the algorithm is .
Tip: Don’t waste time on complex proofs if it isn’t an important part of the interview.
During an interview, correctly deciphering the exact time of an algorithm like the one used to solve this problem isn't usually expected. In situations like this, you can instead make an educated guess about how the algorithm's runtime would grow with larger inputs based on the behavior of the algorithm. Mention any assumptions you make when discussing your estimates. It might also be helpful to mention what parts of the problem or the solution make it difficult to analyze the time complexity. In this problem, it’s initially unclear how many steps the happy number process would take before reaching 1 or revealing a cycle.