Math | Simple solution with 100% beaten
IntuitionThe problem is about repeatedly replacing a number n with the sum of its prime factors until it becomes stable (meaning the sum of its prime factors eq
# Intuition
The problem is about repeatedly replacing a number `n` with the sum of its prime factors until it becomes stable (meaning the sum of its prime factors equals itself).  
The intuition is that prime factorization helps us break the number into its smallest building blocks, and summing them gradually reduces the number or stabilizes it.

# Approach
- Loop until the number becomes stable (i.e., the sum of its prime factors equals the number itself).
- For each iteration:
  - Factorize the number `n` by dividing it by all possible factors starting from 2.
  - Sum up all prime factors.
  - If the sum equals the original number, return the number (it's stable).
  - Otherwise, set `n` to the sum and repeat.

# Complexity
- **Time complexity:**  
  $$O(\sqrt{n} \cdot \log n)$$  
  Each factorization takes $$O(\sqrt{n})$$ time, and since we repeat the process until stabilization, the total time is multiplied by the number of iterations, which is roughly $$O(\log n)$$ in the worst case.

- **Space complexity:**  
  $$O(1)$$  
  We use only a constant amount of extra space.

# Code
```cpp
class Solution {
public:
    int smallestValue(int n) {
        while (true) {
            int sum = 0, x = n;
            for (int i = 2; i * i <= n; ++i) {
                while (n % i == 0) {
                    sum += i;
                    n /= i;
                }
            }
            if (n > 1) sum += n;
            if (sum == x) return x;
            n = sum;
        }
    }
};

['C++']