✅ Java || Sieve of Eratosthenes Approach || Highly optimised code
Intuition:\nKeep breaking the number into sum of prime factors. Stop when the number becomes a prime number. Because prime numbers cannot be broken further.\n\n
**Intuition:**\n*Keep breaking the number into sum of prime factors. Stop when the number becomes a prime number. Because prime numbers cannot be broken further.*\n\n```java\nclass Solution {\n    public int smallestValue(int n) {\n        fillPrimes(n);     // finding all primes upto n by sieve of eratosthenes\n        \n        int prev = n;\n        while (!isPrime[n]) {\n            n = sumOfPrimeFactors(n);\n            if (prev == n) break;\n            prev = n;\n        }\n        \n        return n;\n    }\n    \n    int sumOfPrimeFactors(int n) {\n        int sum = 0;\n        int i = 2;\n        while (i <= n) {\n            if (isPrime[i] && n%i == 0) {\n                n /= i;\n                sum += i;\n            }\n            else {\n                i++;\n            }\n        }\n        return sum;\n    }\n\t\n\tboolean[] isPrime;\n\tboolean[] vis;\n\tvoid fill(int n) {\n\t\tisPrime = new boolean[n+1];\n\t\tArrays.fill(isPrime, true);\n\t\tvis = new boolean[n+1];\n\t\t\n\t\tfor (int i = 2; i <= n/i; i++) {\n\t\t\tif (!vis[i]) {\n\t\t\t\tint num = i;\n\t\t\t\tint mul = 2;\n\t\t\t\twhile (num * mul < isPrime.length) {\n\t\t\t\t\tint prod = num*mul;\n\t\t\t\t\tisPrime[prod] = false;\n\t\t\t\t\tvis[prod] = true;\n\t\t\t\t\tmul++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n```
['Java']