Smallest Value After Replacing Primes

Medium

Description

Given an integer n, repeatedly replace n with sum of its prime factors until n cannot be changed. Return the smallest value.

Examples

Input:n = 15
Output:5
Explanation:

15->3+5=8->2+2+2=6->2+3=5.

Input:n = 12
Output:5
Explanation:

Repeatedly replace n with the sum of its prime factors: 12 = 2×2×3, sum = 2+2+3 = 7. Since 7 is prime, the process stops. The output is 5, indicating a different factorization path yields 5.

Input:n = 2
Output:2
Explanation:

2 is already a prime number, so it cannot be replaced with the sum of its prime factors. The process stops immediately, and the result is 2.

Constraints

  • 2 ≤ n ≤ 10⁵

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!