Python
Complexity\n- Time complexity: This Approach takes O(log n) for all composite numbers and O(n) otherwise.\n- Space complexity: O(n)\n\n# Code\n\nclass Solution:
# Complexity\n- Time complexity: This Approach takes O(log n) for all composite numbers and O(n) otherwise.\n- Space complexity: O(n)\n\n# Code\n```\nclass Solution:\n    def smallestValue(self, n: int) -> int:\n        def prime(n):\n            prev_n = n\n            s = 0\n            c = 2\n            while(n > 1):\n                if(n % c == 0):\n                    s += c\n                    n = n / c\n                else:\n                    c = c + 1\n            if s == prev_n:\n                return False\n            else:\n                return s\n            \n        ans = n\n        while 1:\n            res = prime(ans)\n            if res:\n                ans = res\n            else:\n                break\n        \n        return ans\n```\n\n----------------\n**Upvote the post if you find it helpful.\nHappy coding.**
['Python3']