[Java] Brute force with math
Intuition\n Describe your first thoughts on how to solve this problem. \nDecrease n with brute force.\n\n# Approach\n Describe your approach to solving the prob
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nDecrease n with brute force.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWatch out the $$4 = 2 * 2$$, decrease n with prime sum.\n\nFor some one want to find the math: [Fundamental theorem of arithmetic](https://en.wikipedia.org/wiki/Fundamental_theorem_of_arithmetic)\n\n# Complexity\n- Time complexity: $$O(N)$$\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    // watch out 4 = 2 * 2\n    if (n == 4) {\n      return 4;\n    }\n\n    while (!prime(n)) {\n      int total = 0;\n      for (int i = 2; i <= n; i++) {\n        if (prime(i)) {\n          while (n % i == 0) {\n            total += i;\n            n /= i;\n          }\n        }\n      }\n      n = total;\n    }\n    return n;\n  }\n\n  private boolean prime(int n) {\n    if (n < 4) {\n      return true;\n    }\n    for (int i = 2; i * i <= n; i++) {\n      if (n % i == 0) {\n        return false;\n      }\n    }\n    return true;\n  }\n}\n\n```
['Math', 'Java']