Java | Simplest solution | beats 100%
Approach\n Describe your approach to solving the problem. \n1. If a number is prime, that is the smallest value itself.\n2. Else Calculate the sum of prime fact
# Approach\n<!-- Describe your approach to solving the problem. -->\n1. If a number is prime, that is the smallest value itself.\n2. Else Calculate the sum of prime factors\n    - Calculate first prime factor.\n    - Add in sum\n    - Calculate remainder\n    - Reapeat above steps till remainder is prime.\n    - Add remainder in sum\n3. if sum is equal to input, it will run indefinitely, so we return the value.\n4. Else we will repeat All above steps for the sum.\n\n# Complexity\n- Time complexity: O(nlogn) (Always better than this)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int smallestValue(int n) {\n        if(isPrime(n)) return n; // otherwise it will run forever\n        int sum = getPrimeFactorSum(n);\n        if(sum == n) return n;  // otherwise it will run forever\n        return smallestValue(sum);\n    }\n    \n    public boolean isPrime(int n) { // to check if number is prime\n        if(n == 2) return true;\n        for(int i = 2; i < Math.sqrt(n) + 1; i++) {\n            if(n % i == 0) return false;\n        }\n        return true;\n    }\n    \n    public int getFirstPrimeFactor(int n) { // to get first prime number\n        if(isPrime(n)) return n;\n        for(int i = 2; i < n; i++) {\n            if(n % i == 0) return i;\n        }\n        return n;\n    }\n\n    public int getPrimeFactorSum(int n) { //sum of prime factors of a number\n        int sum = 0;\n        while(!isPrime(n)) {\n            int m = getFirstPrimeFactor(n);\n            n /= m;\n            sum += m;\n        }\n        sum += n;\n        return sum;\n    }\n    \n}\n```
['Java']