Problem
You are given a positive integer n
.
Continuously replace n
with the sum of its prime factors.
- Note that if a prime factor divides
n
multiple times, it should be included in the sum as many times as it dividesn
.
Return **the smallest value *n
* will take on.**
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.
Constraints:
2 <= n <= 105
Solution (Java)
class Solution {
public int smallestValue(int n) {
if(isPrime(n)) return n; // otherwise it will run forever
int sum = getPrimeFactorSum(n);
if(sum == n) return n; // otherwise it will run forever
return smallestValue(sum);
}
public boolean isPrime(int n) { // to check if number is prime
if(n == 2) return true;
for(int i = 2; i < Math.sqrt(n) + 1; i++) {
if(n % i == 0) return false;
}
return true;
}
public int getFirstPrimeFactor(int n) { // to get first prime number
if(isPrime(n)) return n;
for(int i = 2; i < n; i++) {
if(n % i == 0) return i;
}
return n;
}
public int getPrimeFactorSum(int n) { //sum of prime factors of a number
int sum = 0;
while(!isPrime(n)) {
int m = getFirstPrimeFactor(n);
n /= m;
sum += m;
}
sum += n;
return sum;
}
}
Explain:
nope.
Complexity:
- Time complexity : O(n).
- Space complexity : O(n).