IntegerPartition 的变体?
IntegerPartitions[n, {3, 10}, Prime ~Array~ 10]
在 Mathematica 中,这将给出获取 n 作为前 10 个素数中的 3 到 10 之和的所有方法的列表,并允许根据需要重复。
如何有效地找到等于 n 的总和,并允许每个元素仅使用一次?
使用前十个素数只是一个玩具示例。我寻求一个对任意参数都有效的解决方案。在实际情况中,即使使用多项式系数,生成所有可能的和也会占用太多内存。
我忘了注明我正在使用 Mathematica 7。
IntegerPartitions[n, {3, 10}, Prime ~Array~ 10]
In Mathematica this will give a list of all the ways to get n as the sum of from three to ten of the first ten prime numbers, allowing duplicates as needed.
How can I efficiently find the sums that equal n, allowing each element to only be used once?
Using the first ten primes is only a toy example. I seek a solution that is valid for arbitrary arguments. In actual cases, generating all possible sums, even using polynomial coefficients, takes too much memory.
I forgot to include that I am using Mathematica 7.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面将构建一棵二叉树,然后对其进行分析并提取结果:
例如,
这并不是非常快,也许算法可以进一步优化,但至少分区数量不会像<代码>整数分区。
编辑:
有趣的是,简单的记忆将解决方案的速度提高了大约两倍,在我之前使用的示例中:
现在,
The following will build a binary tree, and then analyze it and extract the results:
For example,
This is not insanely fast, and perhaps the algorithm could be optimized further, but at least the number of partitions does not grow as fast as for
IntegerPartitions
.Edit:
It is interesting that simple memoization speeds the solution up about twice on the example I used before:
Now,
可以使用求解整数,乘数限制在 0 和 1 之间。我将展示一个具体示例(前 10 个素数,加到 100),但为此制定通用过程很容易。
输出[178]= {0.004, {{7, 11, 13, 17, 23, 29},
{5, 11, 13, 19, 23, 29}, {5, 7, 17, 19, 23, 29},
{2, 5, 11, 13, 17, 23, 29}, {2, 3, 11, 13, 19, 23, 29},
{2, 3, 7, 17, 19, 23, 29}, {2, 3, 5, 7, 11, 13, 17, 19, 23}}}
---编辑---
要在版本 7 中执行此操作,需要使用“Reduce”而不是“Solve”。我会将其捆绑在一个函数中。
这是 Leonid Shifrin 的示例:
Out[128]= {1.80373, 4660}
不如树代码快,但仍然(我认为)合理的行为。至少,不是明显不合理。
---编辑结束---
Daniel Lichtblau
沃尔夫勒姆研究公司
Can use Solve over Integers, with multipliers constrained between 0 and 1. I'll show for a specific example (first 10 primes, add to 100) but it is easy to make a general procedure for this.
Out[178]= {0.004, {{7, 11, 13, 17, 23, 29},
{5, 11, 13, 19, 23, 29}, {5, 7, 17, 19, 23, 29},
{2, 5, 11, 13, 17, 23, 29}, {2, 3, 11, 13, 19, 23, 29},
{2, 3, 7, 17, 19, 23, 29}, {2, 3, 5, 7, 11, 13, 17, 19, 23}}}
---edit---
To do this in version 7 one would use Reduce instead of Solve. I'll bundle this in one function.
Here is Leonid Shifrin's example:
Out[128]= {1.80373, 4660}
Not as fast as the tree code, but still (I think) reasonable behavior. At least, not obviously unreasonable.
---end edit---
Daniel Lichtblau
Wolfram Research
我想提出一个解决方案,其精神与 Leonid 的解决方案类似,但更短且内存消耗更少。该代码不是构建树并对其进行后处理,而是遍历树并在找到解决方案时
Sow
:此代码比 Leonid 慢
,但使用的内存大约少 6 倍,因此允许走得更远。
I would like to propose a solution, similar in spirit to Leonid's but shorter and less memory intensive. Instead of building the tree and post-processing it, the code walks the tree and
Sow
s the solution when found:This code is slower than Leonid's
but uses about >~ 6 times less memory, thus allowing to go further.