You are given a positive integer n. Your task is to repeatedly replace n with the sum of its prime factors until you reach the smallest possible value.
Important note: If a prime factor divides n multiple times, it should be included in the sum as many times as it divides n. For example, if n = 12 = 2² × 3, then the sum of prime factors is 2 + 2 + 3 = 7.
The process continues until n becomes a prime number (which cannot be reduced further) or reaches a stable value. Return the smallest value that n will eventually reach.
Example: Starting with n = 15:
• 15 = 3 × 5 → sum = 3 + 5 = 8
• 8 = 2³ → sum = 2 + 2 + 2 = 6
• 6 = 2 × 3 → sum = 2 + 3 = 5
• 5 is prime, so we stop.
The answer is 5.
Input & Output
Visualization
Time & Space Complexity
For each iteration, we check divisors up to √n, and we might have multiple iterations
Only using a few variables to store the current number and sum
Constraints
- 1 ≤ n ≤ 105
- n is a positive integer
- The process will always terminate in a finite number of steps