easy solution using recursion and maths
\n\n# Complexity\n- Time complexity:\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:\n Add your space complexity here, e.g. O(n) \n\n# Code\n
\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n\n    // function for checking prime\n    bool isPrime(int n) {\n        if(n==1) return false;\n        for(int i = 2; i <= sqrt(n); i++) {\n            if(n%i == 0) return false;\n        }\n        return true;\n    }\n\n    int smallestValue(int n) {\n        if(isPrime(n)) return n;\n        int sum = 0;\n        \n        // adding prime factors before sqrt(n)\n        for(int i = 2; i < sqrt(n); i++) {\n            if(n%i == 0 && isPrime(i)) {\n                int m = n;\n                while(m % i == 0) {\n                    sum += i;\n                    m /= i;\n                }\n            }\n        }\n        // adding prime factor after sqrt(n)\n        for(int i = 2; i <= sqrt(n); i++) {\n            if(n % (n/i) == 0 && isPrime(n/i)) {\n                int m = n;\n                while(m % (n/i) == 0) {\n                    sum += (n/i);\n                    m /= (n/i);\n                }\n            }\n        }\n        \n        if(sum == n) return n; // special case when n = 4\n        return smallestValue(sum);\n    }\n};\n```
['C++']