# Find minimum sum of factors of number using C++.

C++Server Side ProgrammingProgramming

Here we will see how to get a minimum sum of factors of a given number. Suppose a number is 12. We can factorize this in different ways −

• 12 = 12 * 1 (12 + 1 = 13)
• 12 = 2 * 6 (2 + 6 = 8)
• 12 = 3 * 4 (3 + 4 = 7)
• 12 = 2 * 2 * 3 (2 + 2 + 3 = 7)

The minimum sum is 7. We will take a number, and try to find the minimum factor sum. To get the minimum factor sum, we have to factorize the number as long as possible. In other words, we can say if we try to find the sum S by adding prime factors, then the sum will be minimized.

## Example

Live Demo

#include<iostream>
using namespace std;
int primeFactorSum(int n) {
int s = 0;
for (int i = 2; i * i <= n; i++) {
while (n % i == 0) {
s += i;
n /= i;
}
}
s += n;
return s;
}
int main() {
int n = 12;
cout << "Minimum sum of factors: " << primeFactorSum(n);
}

## Output

Minimum sum of factors: 7
Published on 30-Oct-2019 06:01:24