<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:
        def is_prime(p):
            # function returns True is p is prime, and false otherwise
            if p==1:
                return False
            if p==2 or p==3:
                return True
            for d in range(2,1+int((1+p)**0.5)):
                if p%d==0:
                    return False
            return True

        if is_prime(n)==True:
            return n
        primes=[]
        for p in range(2,n): #1+int((1+n)**0.5)):
            if is_prime(p)==True:
                primes+=[p]

        def prime_sum(m):
            I=0
            total=0
            while m>1 and I<len(primes) and primes[I]<=m: #primes[I]<=1+int((1+m)**0.5):
                p=primes[I]
                p_counter=0
                while m%p==0:
                    m=m//p
                    p_counter+=1
                total+=p*p_counter
                I+=1
            return total
        p_sum=prime_sum(n)
        prev=n
        while prev!=p_sum:
            prev=p_sum
            p_sum=prime_sum(p_sum)
        #print(prime_sum(15),prime_sum(4),prime_sum(101))


        #print(is_prime(5),is_prime(17),is_prime(24))

        return p_sum

16900