【问题标题】:Product partitions产品分区
【发布时间】:2011-06-27 09:25:56
【问题描述】:

我正在寻找有关“产品分区”的信息(我不知道正式名称)
在“经典”分区中,我们将正整数的分解作为总和进行搜索:

Partition(5)   
         5  
       1 4  
       2 3  
     1 1 3  
     1 2 2  
   1 1 1 2  
 1 1 1 1 1  

我想找到所有的分解作为产品:

ProductPartition(36)  
      36  
    2 18  
    3 12  
    4 9  
     6 6  
   2 2 9  
   2 3 6  
   3 3 4  
 2 2 3 3  

我有一个递归解决方案,但效率不够。
非常感谢您提供任何信息。

菲利普
附言
这是我的解决方案(C#):

/// <summary>
/// Products Partition 
/// ProductPartition(24) = (24)(2 12)(3 8)(4 6)(2 2 6)(2 3 4)(2 2 2 3)
/// </summary>
/// <param name="N"></param>
/// <returns></returns>
private List<List<long>> ProductPartition(long N)
{
    List<List<long>> result = new List<List<long>>();
    if (N == 1)
    {
        return result;
    }
    if (ToolsBox.IsPrime(N))
    {
        result.Add(new List<long>() { N });
        return result;
    }

    long[] D = ToolsBox.Divisors(N); // All divisors of N
    result.Add(new List<long>() { N });
    for (int i = 0; i < D.Length - 1; i++)
    {
        long R = N / D[i];
        foreach (List<long> item in ProductPartition(D[i]))
        {
            List<long> list = new List<long>(item);
            list.Add(R);
            list.Sort();
            result.Add(list);
        }
    }

    // Unfortunatly, there are duplicates
    result = L.Unique(result, Comparer).ToList();
    return result;
}  

---------------------------------------------- ( 7 月,10)
尽管此处发布了各种答案,但我仍然遇到性能问题。
如果素数是 { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 },并且我将我的版本应用于素数的前 N ​​个元素的乘积,我得到的结果是:

 N  ProductPartition    ms
 1  Count: 1    CPU:7
 2  Count: 2    CPU:10
 3  Count: 5    CPU:1
 4  Count: 15   CPU:6
 5  Count: 52   CPU:50
 6  Count: 203  CPU:478
 7  Count: 877  CPU:7372
 8  Count: 4140 CPU:56311
 9  Abort after several minutes...

我相信还有更好的。
如果这个功能已经被研究过并且我在哪里可以找到信息,没有人回答我。
我在互联网上尝试了几次搜索都是徒劳的......

再次感谢您的帮助。
菲利普

【问题讨论】:

  • 您想要做的称为“素数分解”。欧几里得算法会有所帮助。 en.wikipedia.org/wiki/Euclidean_algorithm
  • 我知道素数分解欧几里得算法,但我不明白这与我的问题有什么关系。我不是要分解主要因素,我想找到乘积为给定整数的所有子集(不包含 1)。
  • 你为什么不向我们展示你目前的代码,并解释问题是什么?
  • 一旦你有了生成这些集合的素数分解几乎是微不足道的(关于重复因子的警告)

标签: combinatorics data-partitioning


【解决方案1】:

http://en.wikipedia.org/wiki/Integer_factorization

http://en.wikipedia.org/wiki/Integer_factorization#General-purpose

如 cmets 中所述,一旦你有一个算入素数的算法:

def allFactors(num):
    primeFactors = algorithm(num)
    return (product(subset) for subset in combinations(primeFactors))

How to get all possible combinations of a list’s elements?

【讨论】:

    猜你喜欢
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多