✅ [Python/C++] prime factorization (explained)
\u2705 IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE.\n*\nThis solution employs prime factorization.\n\n\nComment. The idea of this solution relies on the fact that 
**\u2705 IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE.**\n****\nThis solution employs prime factorization.\n****\n\n**Comment.** The idea of this solution relies on the fact that a number is not less than the sum of its primes. Equality is attained for the prime numbers themselves and also for the number 4. Thus we can iteratively decompose `n` into primes and compute their sum until the sequence stabilizes.\n\n**Python #1.**\n```\nclass Solution:\n    def smallestValue(self, n: int) -> int:\n        \n        def primes(n, s=0):\n            for i in range(2,n+1):\n                while n % i == 0:\n                    s += i\n                    n //= i\n            return s\n        \n        while n != (n:=primes(n)): pass\n        \n        return n\n```\n\n**Python #2.** This can be compactified even further.\n```\nclass Solution:\n    def smallestValue(self, n: int) -> int:\n\n        m, s = n, 0\n        for i in range(2,n+1):\n            while m % i == 0:\n                s += i\n                m //= i\n\n        return s if s == n else self.smallestValue(s)\n```\n\n**C++.** The solution above compressed into three lines of code.\n```\nclass Solution \n{\npublic:\n    int smallestValue(int n, int s = 0) \n    {\n        for (int i = 2, m = n; i <= n; i++)\n            for(; m % i == 0; s += i, m /= i);\n        return s == n ? n : smallestValue(s);\n    }\n};\n```
[]