Smallest Value After Replacing With Sum of Prime Factors - Problem
You are given a positive integer n.
Continuously replace n with the sum of its prime factors.
Note: If a prime factor divides n multiple times, it should be included in the sum as many times as it divides n.
Return the smallest value n will take on.
Input & Output
Example 1 — Basic Composite Number
$
Input:
n = 15
›
Output:
5
💡 Note:
15 = 3 × 5, sum = 8. Then 8 = 2³, sum = 6. Then 6 = 2 × 3, sum = 5. Since 5 is prime, return 5.
Example 2 — Already Prime
$
Input:
n = 3
›
Output:
3
💡 Note:
3 is already prime, so it cannot be reduced further. Return 3.
Example 3 — Power of Prime
$
Input:
n = 8
›
Output:
5
💡 Note:
8 = 2³, sum = 2+2+2 = 6. Then 6 = 2×3, sum = 2+3 = 5. Since 5 is prime, return 5.
Constraints
- 2 ≤ n ≤ 1000
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code