FactorInteger 的替代形式? (数学)
在 Mathematica 中
a = FactorInteger[44420069694]
赋值
{{2, 1}, {3, 1}, {7, 1}, {11, 2}, {13, 1}, {23, 2}, {31, 1}, {41, 1}}
给 a。 现在,我希望扩展每个列表,而不是带有指数的因子。 上面的因式分解将变成
{2, 3, 7, 11, 11, 13, 23, 23, 31, 41}
我编写的以下函数:
b = {}; Do[Do[b = Append[b, a[[i]][[1]]], {a[[i]][[2]]}], {i, Length[a]}]
但如果你问我它看起来很丑陋。 肯定有一种更简洁的方法来实现这一目标吗?
In Mathematica
a = FactorInteger[44420069694]
assigns
{{2, 1}, {3, 1}, {7, 1}, {11, 2}, {13, 1}, {23, 2}, {31, 1}, {41, 1}}
to a. Now instead of the factors with their exponents I would like each of those lists expanded. The above factorization would then become
{2, 3, 7, 11, 11, 13, 23, 23, 31, 41}
I wrote the following function:
b = {}; Do[Do[b = Append[b, a[[i]][[1]]], {a[[i]][[2]]}], {i, Length[a]}]
but if you ask me it looks fugly. There sure must be a neater way to do achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,例如:
Yes, for example:
Mathematica 6 或更高版本中的另一种方法。
In:=
Flatten[ConstantArray @@@ a]
Out=
{2, 3, 7, 11, 11, 13, 23, 23, 31, 41}
更短:
Join @@ ConstantArray @@@ a
发布方法的速度比较
使用这些函数(按照发布的顺序):
并分别为它们分配字母 Z、D、G、M、S ,这是其效率的时序图。
首先,为了增加输入中的列表数量:
其次,为了增加指数(重复长度)每个列表:
请注意,这些图表是对数的。 越低越好。
Yet another way in Mathematica 6 or later.
In:=
Flatten[ConstantArray @@@ a]
Out=
{2, 3, 7, 11, 11, 13, 23, 23, 31, 41}
even shorter:
Join @@ ConstantArray @@@ a
A speed comparison of methods posted
Using the these functions (in the order they were posted):
and assigning them the letters Z, D, G, M, S respectively, here are Timing charts of their efficiency.
First, for increasing number of lists in the input:
Second, for increasing exponent (length of repetition) in each list:
Note that these charts are logarithmic. Lower is better.
这是另一种方法:
可以用 lambda 函数将其压缩为:
如果您喜欢这种事情,zvrba 的答案也可以压缩一点:(
现在我看看它,我想我的版本是zvrba 的非常小的变体。)
Here's another way to do it:
Which can be condensed with a lambda function to:
zvrba's answer can also be condensed a bit, if you're into that sort of thing:
(Now that I look at that, I guess my version is a very minor variant on zvrba's.)
您还可以使用:
更新2017/10/18:
正如科里·沃克(Cory Walker)指出的那样,“在两个不同素因数的情况下”,我上面的答案失败了。 本次更新修复了这个问题:
请注意,Mr Wizard 所做的基准测试是针对本次更新之前的原始版本进行的。
You could also use:
UPDATE 2017/10/18:
My answer above fails "in the case of two distinct prime factors" as pointed out by Cory Walker. This update fixes it:
notice that the benchmark done by Mr Wizard was done with the original version before this update.
也可以使用数组来处理答案。 这是执行此操作的简短代码:
One can also use Array to process the answer. Here is a short code doing this: