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.

Example:

Input: n = 23
Output: True

Explanation: 222^2 + 323^2 = 13 ⇒ 121^2 + 323^2 = 10 ⇒ 121^2 + 020^2 = 1

In Plain English

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.

🌱 Real-world analogy: a hallway of doors

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.

Intuition

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:

Image represents two distinct cases illustrating data flow, possibly representing different coding patterns or algorithms.  Case 1 shows a linear sequence of four circular nodes labeled 23, 13, 10, and 1, respectively.  Each node is connected to the next by a unidirectional arrow, indicating a sequential flow of information from 23 to 1. Case 2 depicts a circular flow of data. Eight circular nodes, labeled 116, 38, 73, 58, 37, 16, 4, and 20, are arranged in a circle.  Unidirectional arrows connect each node to the next in a clockwise direction, forming a closed loop.  Additionally, a separate circular sequence of three nodes (89, 145, and 42) is connected to the main circular sequence via arrows from node 58 to 89 and from node 42 to node 20.  The numbers within each node likely represent data values or states, and the arrows signify the direction of data processing or transformation.

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:

  1. The modulo operation (x % 10) is used to extract the last digit of a number x.

  2. 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:

Image represents a flowchart illustrating the `get_next_num(x)` function, which takes an integer `x` (initially 123) as input and calculates a new number (`next_num`). The process involves iteratively extracting digits from `x` using the modulo operator (`%`) and integer division (`/`).  First, the rightmost digit (3) is obtained via `x % 10`, then `x` is updated to `x / 10` (12). This process repeats, extracting the next digit (2) and then (1). Each extracted digit is circled in orange.  These digits are then used to calculate `next_num` by summing their squares (3² + 2² + 1² = 14). The iteration stops when `x` becomes 0.  Arrows indicate the flow of data, showing how the extracted digits (`digit`) are derived and subsequently used in the final calculation of `next_num`. The function's output, `next_num`, is explicitly calculated as 14 at the bottom.

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:

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.

Implementation

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_num
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;
}

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;
}

Complexity Analysis

Time complexity: The time complexity of happy_number is O(log(n))O(\log(n)). 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 O(1)O(1).

Dry Runs

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.

✅ Dry run 1 (valid) — n = 19 → 82 → 68 → 100 → 1, so fast reaches 1 → true.

slow (+1) fast (+2)

The sequence (each arrow applies digit-square-sum):
19
82
68
100
1
1 (self)

The fast / slow trace

slow applies next once; fast applies it twice. Stop when fast hits 1 (happy) or slow == fast (loop).
iterationslowfastfast == 1?
start1982no
182100no
2681yes → 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 == 1true.

📈 Dry run 2 (invalid) — n = 2 falls into a loop that never reaches 1 → false.

slow (+1) fast (+2) the repeating cycle

The sequence for n = 2 never hits 1 — it falls into an 8-number cycle (4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → back to 4):
2
4
16
37
58
89
145
42
20
back to 4

The fast / slow trace

slow applies next once; fast twice. There is no 1 to reach, so eventually the hare laps the tortoise: slow == fast at 42.
iterationslowfastslow == fast?
start24no
1437no
21689no
33742no
4584no
58937no
614589no
74242yes (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.

The same steps in each language

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.

Interview Tip

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.

Happy Number Time Complexity Analysis

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 nn as the result obtained by summing the squares of the digits of nn.

1) Upper limit for the next number For any number nn 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

DigitsLargest NumberNext Number
1981
299162
3999243
49999324
599999405
6999999486
.........

 

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 nn larger than 243 will have the next number smaller than nn. 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 O(1)O(1).

5) Time complexity for numbers greater than 243 The number of digits in a number is approximately equal to log(n)\log(n) (base 10). So, the calculation of the next number of nn will take approximately log(n)\log(n) steps (i.e., get_next_num will take log(n)\log(n) steps to execute).

Let's call nn’s next number n2n^2. The next number after n2n^2 (n3n^3) will take approximately log(n2)\log(n^2) steps to calculate. The next number after n3n^3 (n4n^4) will take approximately log(n3)\log(n^3) steps to calculate, and so on. From this, we can summarize the time complexity of this process as O(log(n)+log(n2)+log(n3)+)O(\log(n)+\log(n^2)+\log(n^3)+…). Since we've established that n>n2>>nkn>n^2>…>n^k where nkn^k is the last number greater than 243, the dominant component of this time complexity is O(log(n))O(\log(n)). So, the time complexity for numbers greater than 243 is O(log(n))O(\log(n)).

Conclusion When nn is less than 243, the time complexity is O(1), and when nn is greater than 243, the time complexity is O(log(n))O(\log(n)). Therefore, the overall time complexity of the algorithm is O(log(n))O(\log(n)).

Interview Tip

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.