O(sqrt(n) * P(n)) C++ solution [Beats 100% of solutions]
Intuition\nFor any prime number p, its prime factors will be \{1,p\}. Ignoring the trivial factor of 1, the sum of its prime factors will always be equal to p i
# Intuition\nFor any prime number $$p$$, its prime factors will be $$\\{1,p\\}$$. Ignoring the trivial factor of $$1$$, the sum of its prime factors will always be equal to $$p$$ itself. Therefore, we can check whether a given number $$n$$ is prime (and therefore satisfies the question)  by checking whether $$n=sum(primeFactors(n))$$\n\n# Approach\nFor taking the prime factors of a number efficiently, we can apply a simple prime factorization algorithm. Take the number $$18$$ for example.\n$$18\\mod2=0$$, therefore, $$18 = \\frac{18}{2} \\times2 =9 \\times 2$$\n$$9\\mod2\\neq0$$. We continue\n$$9\\mod3=0$$\n$$...$$\n$$18 = 3\\times3\\times2$$\n\nWe can also show that for any number $$n$$ there will be at most one prime factor greater than $$\\sqrt{n}$$. Since any number can be represented by $$n=a\\times p$$, where $$p$$ is a prime factor of $$n$$ and $a=\\frac{prod(primeFactors(n))}{p}$, $p=\\frac{n}{a}$. If there is any other prime factor greater than $\\sqrt{n}$, $p<\\sqrt{n}$. Therefore, there can be only one $p$ such that $p>\\sqrt{n}$.\n\nFrom that, we can just reduce $n$ with prime factors smaller than $\\sqrt{n}$ and then just add the prime factors obtained with the remainder. That gives us an overall complexity of $P(n)\\times\\sqrt{n}$. Why the $P(n)$? Because that will be the amount of times that function will run; I have no idea whatsoever how much times it will run. I don\'t think anyone does. It\'s related to prime numbers, after all\n\n# Complexity\n- Time complexity: $$O(\\sqrt{n} \\times P(n))$$ \n\n- Space complexity: $$O(1)$$\n\n# About memory\n"Oh, using functions is going to increase memory usage!" and not using them makes the code ugly. Speed is all that matters, folks!\n\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    inline int getsum(int n)\n    {\n        int sum = 0;\n        for(int i=2;i*i<=n;i++)\n        {\n            if(n%i==0)\n            {\n                while(n%i==0) {\n                    sum+=i;\n                    n/=i;\n                }\n                \n            }\n        }\n        if(n>1) sum += n;\n        return sum;\n    }\n    int smallestValue(int n) {\n        while(n!=(n=getsum(n))){}\n\n        return n;\n    }\n};\n```
['C++']