Java程序查找数字因子的最小总和
给定一个数,求其因子的最小和。
例子:
Input : 12
Output : 7
Explanation:
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
// 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));
}
}
输出:
7
有关详细信息,请参阅有关查找数字因子的最小和的完整文章!