Simple and Efficient C++ Solution
\ntypedef long long ll;\nclass Solution {\npublic:\n    ll f(int n)\n    {\n        ll s=0;\n        ll y=n;\n        for(int i=2;i<y;i++)\n        {\n         
```\ntypedef long long ll;\nclass Solution {\npublic:\n    ll f(int n)\n    {\n        ll s=0;\n        ll y=n;\n        for(int i=2;i<y;i++)\n        {\n            if(isprime(i))\n            {\n                while(n%i==0)\n                {\n                    n/=i;\n                     s+=i;\n                    if(isprime(n))\n                    {\n                        s+=n;\n                        return s;\n                    }\n                }\n            }\n            \n        }\n        return s;\n    }\n    bool isprime(int n)\n    {\n        for(int i=2;i<n;i++)\n        {\n            if(n%i==0)\n                return false;\n        }\n        return true;\n    }\n    int smallestValue(int n) {\n        ll mini=n;\n        ll s=n;\n        while(!isprime(n))\n        {\n            ll x=f(n);\n            n=x;\n            mini=min(mini,x);\n            if(s==x)\n            return mini;\n            s=x;\n           \n        }\n        return mini;\n    }\n};\n```
[]