【问题标题】:How to find the factors of a number given the prime factorization?给定素数分解,如何找到数字的因数?
【发布时间】:2019-05-30 01:56:17
【问题描述】:

如果我以[2, 2, 3, 5, 5] 的形式给出一个数字的素因数分解,我将如何找到[1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 25, 30, 50, 60, 75, 100, 150, 300] 形式的所有因数

我试图通过迭代循环来做到这一点,但据我所知,它并没有点击获取数字的方法,结果是两个以上的数字相乘

def find_factors(pfacts):
    pfacts = [1] + pfacts
    temp = []
    for i in pfacts:
        for j in pfacts[i+1:]:
            if i * j not in temp:
                temp.append(i * j)
    return [1] + temp

我知道这不是正确的做法,因为它只能找到一小部分因素

[1, 2, 3, 5, 6, 10, 15]

【问题讨论】:

  • 只需将素数分解中的所有数相乘,您将得到一个数并找到该数的因数。这是获取因子stackoverflow.com/questions/6800193/… 的一个很好的答案。
  • 以上评论导致的解决方案效率低得多,因为该答案中的最佳答案具有与 N 的平方根成比例的解决方案,其中 N 是您的因素的乘积
  • 要厌倦的一件事是您必须处理的因素组合呈指数增长。考虑 [2]*100,它只有 101 个因子,但使用 itertools.combinations 将返回 2^100 个元素(给定或取因子 2)
  • @Untitled123 那么找到因子的更好方法是什么?
  • @Rahul ,您发布的链接有很好的答案,说明如何仅给定一个数字 N 找到因子。在这里,我们已经给出了它的所有素因子,只需将它们组合起来即可找到休息。我认为到目前为止发布的所有答案都可以做到这一点。考虑一个数 N,它是 P 和 Q 的巨大素数(10^100 的数量级)的乘积。找到 N 的因数(不知道 P 或 Q)是一个非常困难且计算量很大的过程,但鉴于素数因数P,Q,不难发现N的因数只有1,P,Q,N。

标签: python


【解决方案1】:

一种方法是将itertools.productnumpy.prodnumpy.power 一起使用:

import numpy as np
from itertools import product

f = [2, 2, 3, 5, 5]
uniq_f = np.unique(f)
counts = np.array(list(product(*(range(f.count(i) + 1) for i in uniq_f))))
sorted(np.prod(np.power(uniq_f, counts), 1))

输出:

[1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 25, 30, 50, 60, 75, 100, 150, 300]

【讨论】:

    【解决方案2】:

    您可以使用itertools.combinations(会给出重复项)和set 过滤掉重复项:

    from itertools import combinations
    from functools import reduce
    from operator import mul
    
    factors = [2, 2, 3, 5, 5]
    
    set(reduce(mul, combination) for i in range (1, len(factors) + 1) for combination in combinations(factors, i))
    

    输出:

    {2, 3, 4, 5, 6, 10, 12, 15, 20, 25, 30, 50, 60, 75, 100, 150, 300}
    

    【讨论】:

      【解决方案3】:

      将所有组合相乘并将它们添加到一个集合中。

      import itertools
      
      def multiplyList(myList):
          # Multiply elements one by one
          result = 1
          for x in myList:
              result = result * x
          return result
      
      factors=set()
      
      stuff = [2, 2, 3, 5, 5]
      for L in range(0, len(stuff)+1):
          for subset in itertools.combinations(stuff, L):
              factors.add(multiplyList(subset))
      
      factors=list(sorted(factors))
      
      print(factors)
      

      如下所示:

      [1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 25, 30, 50, 60, 75, 100, 150, 300]
      

      【讨论】:

        【解决方案4】:

        我对 python 比较陌生,所以我无法理解已经发布的一些内容。这是我的答案,它比较长,但对于初学者来说可能更容易理解。

        import numpy as np
        import itertools
        
        factors = [2, 2, 3, 5, 5]
        
        al = []
        for i in range(len(factors)):
            for combo in itertools.combinations(factors,i):
                al.append(np.prod(combo))
        
        print(np.unique(al))
        

        输出:

        [  1.   2.   3.   4.   5.   6.  10.  12.  15.  20.  25.  30.  50.  60.
          75. 100. 150.]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-04-08
          • 1970-01-01
          • 2012-02-21
          • 2021-05-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多