Python 程序查找数字的最小因子之和


在本文中,我们将学习针对下面给出的问题陈述的解决方案 −

问题陈述

给定一个数字输入,找出给定数字的因子的最小和。

这里我们将计算所有因子及其对应的和,然后找出其中的最小值。

因此,要找出数字乘积的最小和,我们找出乘积质因子的和。

以下是针对该问题的迭代实现 −

示例

 在线演示

#iterative approach
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_
# Driver Code
num = 12
print (findMinSum(num))

Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.

输出

7

所有变量都在全局框架中声明,如下图所示 −

结论

在本文中,我们学习了找出数字的因子的最小和的方法。

更新于: 26-Sep-2019

576 次浏览

开启您的 职业生涯

完成课程获得认证

开始
广告