#2507

Smallest Value After Replacing With Sum of Prime Factors

specialist · 875 · lc medium +31 · verified · 49.8% accepted · 457 likes · top 37%

Description

Given a positive integer n, repeatedly replace n with the sum of all its prime factors (counting each factor with multiplicity) until the value stops changing. Return that fixed-point value.

Example 1:

Input: n = 15
Output: 5
Explanation: Initially, n = 15.
15 = 3 * 5, so replace n with 3 + 5 = 8.
8 = 2 * 2 * 2, so replace n with 2 + 2 + 2 = 6.
6 = 2 * 3, so replace n with 2 + 3 = 5.
5 is the smallest value n will take on.

Example 2:

Input: n = 3
Output: 3
Explanation: Initially, n = 3.
3 is the smallest value n will take on.

Code

1
2
3