# Find minimum sum of factors of number

Given a number, find minimum sum of its factors.

Examples:

Input : 12 Output : 7Explanation:Following are different ways to factorize 12 and sum of factors 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 Therefore minimum sum is 7 Input : 105 Output : 15

To minimize sum, we must factorize factors as long as possible. With this process, we prime factors. So to find minimum sum of product of number, we find sum of prime factors of product.

## C++

`// CPP program to find minimum ` `// sum of product of number ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// To find minimum sum of ` `// product of number ` `int` `findMinSum(` `int` `num) ` `{ ` ` ` `int` `sum = 0; ` ` ` ` ` `// Find factors of number ` ` ` `// and add to the sum ` ` ` `for` `(` `int` `i = 2; i * i <= num; i++) { ` ` ` `while` `(num % i == 0) { ` ` ` `sum += i; ` ` ` `num /= i; ` ` ` `} ` ` ` `} ` ` ` `sum += num; ` ` ` ` ` `// Return sum of numbers ` ` ` `// having minimum product ` ` ` `return` `sum; ` `} ` ` ` `// Driver program to test above function ` `int` `main() ` `{ ` ` ` `int` `num = 12; ` ` ` ` ` `cout << findMinSum(num); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find minimum ` `// sum of product of number ` ` ` `public` `class` `Main { ` ` ` ` ` `// To find minimum sum of ` ` ` `// product of number ` ` ` `static` `int` `findMinSum(` `int` `num) ` ` ` `{ ` ` ` `int` `sum = ` `0` `; ` ` ` ` ` `// Find factors of number ` ` ` `// and add to the sum ` ` ` `for` `(` `int` `i = ` `2` `; i * i <= num; i++) { ` ` ` `while` `(num % i == ` `0` `) { ` ` ` `sum += i; ` ` ` `num /= i; ` ` ` `} ` ` ` `} ` ` ` `sum += num; ` ` ` ` ` `// Return sum of numbers ` ` ` `// having minimum product ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver program to test above function ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `num = ` `12` `; ` ` ` `System.out.println(findMinSum(num)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python

`# Python program to find minimum ` `# sum of product of number ` ` ` `# To find minimum sum of ` `# product of number ` `def` `findMinSum(num): ` ` ` `sum` `=` `0` ` ` ` ` `# Find factors of number ` ` ` `# and add to the sum ` ` ` `i ` `=` `2` ` ` `while` `(i ` `*` `i <` `=` `num): ` ` ` `while` `(num ` `%` `i ` `=` `=` `0` `): ` ` ` `sum` `+` `=` `i ` ` ` `num ` `/` `=` `i ` ` ` `i ` `+` `=` `1` ` ` `sum` `+` `=` `num ` ` ` ` ` `# Return sum of numbers ` ` ` `# having minimum product ` ` ` `return` `sum` ` ` `# Driver Code ` `num ` `=` `12` `print` `findMinSum(num) ` ` ` `# This code is contributed by Sachin Bisht ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find minimum ` `// sum of product of number ` `using` `System; ` ` ` `public` `class` `GFG { ` ` ` ` ` `// To find minimum sum of ` ` ` `// product of number ` ` ` `static` `int` `findMinSum(` `int` `num) ` ` ` `{ ` ` ` `int` `sum = 0; ` ` ` ` ` `// Find factors of number ` ` ` `// and add to the sum ` ` ` `for` `(` `int` `i = 2; i * i <= num; i++) { ` ` ` `while` `(num % i == 0) { ` ` ` `sum += i; ` ` ` `num /= i; ` ` ` `} ` ` ` `} ` ` ` `sum += num; ` ` ` ` ` `// Return sum of numbers ` ` ` `// having minimum product ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `num = 12; ` ` ` `Console.Write(findMinSum(num)); ` ` ` `} ` ` ` `} ` ` ` `// This Code is contributed by Nitin Mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find minimum ` `// sum of product of number ` ` ` `// To find minimum sum of ` `// product of number ` `function` `findMinSum(` `$num` `) ` `{ ` ` ` `$sum` `= 0; ` ` ` ` ` `// Find factors of number ` ` ` `// and add to the sum ` ` ` `for` `(` `$i` `= 2; ` `$i` `* ` `$i` `<= ` `$num` `; ` `$i` `++) ` ` ` `{ ` ` ` `while` `(` `$num` `% ` `$i` `== 0) ` ` ` `{ ` ` ` `$sum` `+= ` `$i` `; ` ` ` `$num` `/= ` `$i` `; ` ` ` `} ` ` ` `} ` ` ` `$sum` `+= ` `$num` `; ` ` ` ` ` `// Return sum of numbers ` ` ` `// having minimum product ` ` ` `return` `$sum` `; ` `} ` ` ` `// Driver Code ` `$num` `= 12; ` ` ` `echo` `(findMinSum(` `$num` `)); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

Output:

7

This article is contributed by **nuclode**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Find number of factors of N when location of its two factors whose product is N is given
- Find sum of odd factors of a number
- Find sum of even factors of a number
- Queries to find whether a number has exactly four distinct factors or not
- Find minimum number to be divided to make a number a perfect square
- Number which has the maximum number of distinct prime factors in the range M to N
- Queries on sum of odd number digit sums of all the factors of a number
- Super Ugly Number (Number whose prime factors are in given set)
- Find minimum possible digit sum after adding a number d
- Find the minimum number of steps to reach M from N
- Find minimum number of coins that make a given value
- Find minimum number of Log value needed to calculate Log upto N
- Find a number X such that (X XOR A) is minimum and the count of set bits in X and B are equal
- Find the minimum number of rectangles left after inserting one into another
- Number with maximum number of prime factors