(Java) || Time complexity: O(log n . √n) || Runtime : 1ms
IntuitionThe problem requires repeatedly reducing a number 𝑛 by replacing it with the sum of its prime factors until it becomes a prime number. The key observat
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires repeatedly reducing a number 𝑛 by replacing it with the sum of its prime factors until it becomes a prime number. The key observation is that during each iteration, 𝑛 gets smaller (or stabilizes) due to the summation process, eventually converging to a prime number.



# Approach
<!-- Describe your approach to solving the problem. -->
Prime Factorization:

Decompose 𝑛 into its prime factors and calculate their sum. This is done by iterating from 2 to 𝑛 and dividing 𝑛 by each factor until it becomes indivisible.
Repeat this process until 𝑛 is a prime number.

Prime Check:

Use a helper function isPrime to check whether a number is prime by iterating up to 𝑛.

Termination Condition:

If the sum of the prime factors equals 𝑛 (i.e., no further reduction is possible), return 𝑛.
Otherwise, update 𝑛 to the computed sum and repeat.


# Complexity
- Time complexity: O(log n . √n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {
    public int smallestValue(int n) {
        while(! isPrime(n)){
            int sum = 0;
            int temp = n;
            for(int i=2; i<=temp; i++){
                while(temp%i == 0){
                    sum += i;
                    temp /= i;
                }
            }
            if(sum == n){
                return n;
            }
            n = sum;
        }
        return n;
    }
    public Boolean isPrime(int n){
        if(n<2){
            return false;
        }
        for(int i=2; i*i<=n; i++){
            if(n%i == 0){
                return false;
            }
        }
        return true;
    }
}
```
['Java']