<p>You are given a positive integer <code>n</code>.</p>

<p>Continuously replace <code>n</code> with the sum of its <strong>prime factors</strong>.</p>

<ul>
	<li>Note that if a prime factor divides <code>n</code> multiple times, it should be included in the sum as many times as it divides <code>n</code>.</li>
</ul>

<p>Return <em>the smallest value </em><code>n</code><em> will take on.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 15
<strong>Output:</strong> 5
<strong>Explanation:</strong> Initially, n = 15.
15 = 3 * 5, so replace n with 3 + 5 = 8.
8 = 2 * 2 * 2, so replace n with 2 + 2 + 2 = 6.
6 = 2 * 3, so replace n with 2 + 3 = 5.
5 is the smallest value n will take on.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> Initially, n = 3.
3 is the smallest value n will take on.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
</ul>


class Solution:
    def smallestValue(self, n: int) -> int:
        m = n**0.5 +1
        nums = [0]*int(m)
        def sp(k):
            if k<6: return k
            m = int(k**0.5)+ 1
            result = 0
            factor = 2
            while factor < m:
                print(factor,result,k)
                if nums[factor] == 0:
                    while k%factor ==0:
                        result += factor
                        k //= factor
                        m = k**0.5 +1
                    p = factor
                    while factor*p<m:
                        nums[factor*p] = 1
                        p += 1     
                factor += 1
                print(factor,result,k)
            return result+k if k-1 else result
        temp = sp(n)
        while temp!=n:
            print(n)
            n = temp
            temp = sp(n)
        return n
                    
                
'''
class Solution:
    def smallestValue(self, n: int) -> int:
        def sp(k):
            if k<=5: return k
            factor = 2
            s = 0
            while k - factor>0:
                if k % factor == 0:
                    s+= factor
                    k //=factor
                else:
                    factor += 1
            return s+factor
        while sp(n)!=n:
            n = sp(n)
        return sp(n)
'''

16900