~20
IntuitionApproachComplexity

Time complexity:
O(log_p(sqrt(n))), p - most common factor except 2


Space complexity:
O(log2(n)) - n - max pow of 2, if all the f
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

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

# Complexity
- Time complexity:
O(log_p(sqrt(n))), p - most common factor except 2

- Space complexity:
O(log2(n)) - n - max pow of 2, if all the factors are '2'

# Code
```typescript []
function smallestValue(n: number): number {
    let res = n
    let min = 10
    let prev = -1

    while (true) {
        prev = res
        const arr = primeFactors(res)
        res = arr.reduce((sum, item) => sum + item, 0)

        if (arr.length === 1 && arr[0] === res || prev === res) {
            break
        }
    }

    return res
};

    function primeFactors(n) {
        const arr = []

    // Print the number of 2s that divide n
    while (n % 2 == 0) {

        // console.log(2 + " ");
        // console.log('z', 2)
        arr.push(2)
        n = Math.floor(n / 2);
    }

    // n must be odd at this point.
    // So we can skip one element
    // (Note i = i +2)

    for (let i = 3; i <= Math.floor(Math.sqrt(n));
         i = i + 2) {

        // While i divides n, print i
        // and divide n
        while (n % i == 0) {

            arr.push(i)
            // console.log('z', i)
            // process.stdout.write(i + " ");
            n = Math.floor(n / i);
        }
    }

    // This condition is to handle the
    // case when n is a prime number
    // greater than 2
    if (n > 2)
        arr.push(n)
        // process.stdout.write(n + " ");
        // console.log('z', n)
        
    return arr//[...new Set(arr)]
}
```
['TypeScript']